Skip to content

Add initial Unicode Support - #649

Open
Jafaral wants to merge 31 commits into
uniconproject:masterfrom
Jafaral:unicode
Open

Jafaral wants to merge 31 commits into
uniconproject:masterfrom
Jafaral:unicode

Conversation

@Jafaral

@Jafaral Jafaral commented Aug 10, 2026

Copy link
Copy Markdown
Member

No description provided.

@greptile-apps

greptile-apps Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Greptile Summary

The PR adds the initial tagged-qualifier implementation of native Unicode support while retaining byte-oriented behavior for untagged strings.

  • Adds Unicode descriptor metadata, compile-time literal tagging, codepoint-aware runtime operations, conversions, and opt-in Unicode I/O.
  • Adds a dedicated Unicode behavioral suite and documents the representation and supported operations in UTR 25.
  • The fixes for all three previously reported Unicode-state and codepoint-count issues are present at the current head.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains from the previously reported findings.

The current variadic concatenation code restores Unicode metadata after length assignment, and substring assignment now scans the complete assembled result before caching its codepoint count, resolving all previously reported failures.

Important Files Changed

Filename Overview
src/h/rmacros.h Defines the tagged string-descriptor layout, masked byte lengths, cached codepoint counts, and shared UTF-8 helpers.
src/icont/tsym.c Detects multibyte UTF-8 literals during translation so Unicode metadata can be carried into generated icode.
src/icont/lcode.c Emits the Unicode qualifier into runtime literal descriptors without changing the literal payload.
src/runtime/fconv.r Preserves Unicode tagging and cached counts across variadic string concatenation, resolving the prior qualifier-ordering issue.
src/runtime/oasgn.r Recomputes Unicode metadata from the complete assembled substring-assignment result, resolving both prior assignment findings.
src/runtime/fmisc.r Adds strict Unicode scalar conversion through uchar() and uord() and updates runtime string construction.
src/runtime/fsys.r Adds opt-in Unicode-aware input handling to supported file and socket paths.
tests/unicode/Makefile Integrates focused Unicode behavior tests with feature-aware skipping for unsupported builds.
doc/utr/utr25.rst Documents the tagged-qualifier representation, language semantics, supported operations, and remaining limitations.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Source string literal] --> B[icont UTF-8 scan]
  B --> C[Tagged icode literal]
  D[File or socket input] --> E{Unicode I/O mode}
  E -->|Enabled| F[Runtime UTF-8 scan]
  E -->|Disabled| G[Byte-oriented string]
  C --> H[Tagged runtime descriptor]
  F --> H
  H --> I[Codepoint-aware size, indexing, scanning, and concatenation]
  G --> J[Byte-oriented operations]
Loading

Reviews (4): Last reviewed commit: "doc: Add native Unicode design as UTR # ..." | Re-trigger Greptile

Comment thread src/runtime/fconv.r Resolved
Comment thread src/runtime/oasgn.r Resolved
Comment thread src/runtime/oasgn.r Resolved
@Jafaral

Jafaral commented Aug 17, 2026

Copy link
Copy Markdown
Member Author

@brucerennie since you did some unicode/utf8 work on the language level, I'd appreciate if you have some feedback on this PR. a preview of the technical report is available here:

https://jafaral.github.io/unicon/doc/utr/html/utr25.html

@Jafaral
Jafaral force-pushed the unicode branch 2 times, most recently from a2d143d to eee9c04 Compare August 21, 2026 04:44
@brucerennie

brucerennie commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

Jafar,

I am working through the above PR and in the file fmisc.r, no merge is possible as the line 3125 has a reference to bloblen but is not found in the latest fmisc.r

Similar problem in fstruct.r. The change is required in line 693 but is found in latest sources at line 908.

Fsys.r also has merge problems - function close and can't find File.fname

Rmisc.r also has merge problems

Rmswin.ri has line 1261 which would appear to need a SetStrLen used

rposix.r has a number of places where SetStrLen might be needed, lines 1132, 1193, 1240, 1270, 4262,

rwindows.r line 587 appears to need SetStrLen

Will continue working through all the changes

@Jafaral

Jafaral commented Aug 24, 2026

Copy link
Copy Markdown
Member Author

@brucerennie, thank you for the feedback. If you tried to merge this into the master branch to try it, then that might explain the issue. I merged other PRs in recent days which created conflict with this PR. I will resolve those and update the PR.

@brucerennie

Copy link
Copy Markdown
Collaborator

Jafar,

Further to the above message, the file oref.r is also merge resistant.

A suggestion though. As I was looking through the changes for StrLen to SetStrLen, I noticed that in many cases there was associated StrLoc. Sometimes the order was SetStrLen followed by StrLoc and in other places Strloc followed by SetStrLen.

To unify the code (so to speak), I believe that it could be beneficial to have both calls being replaced by a single macro that performs the current two macros. It is only a suggestion though.

@Jafaral

Jafaral commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

Jafar,

Further to the above message, the file oref.r is also merge resistant.

I fixed the conflicts and pushed the update as you were writing the comment.

A suggestion though. As I was looking through the changes for StrLen to SetStrLen, I noticed that in many cases there was associated StrLoc. Sometimes the order was SetStrLen followed by StrLoc and in other places Strloc followed by SetStrLen.

To unify the code (so to speak), I believe that it could be beneficial to have both calls being replaced by a single macro that performs the current two macros. It is only a suggestion though.

We do have that already actually, I will make a sweep commit to use that where possible.

@Jafaral

Jafaral commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@brucerennie I updated a lot of locations to use MakeStr which does both StrLen and SetStrLen

@brucerennie

Copy link
Copy Markdown
Collaborator

@Jafaral I have merged the unicode PR into a current copy of unicon. Looking at the tests, I don't see anything untowards. I did a test of the current ui program and it is not unicode aware in terms of the display. I will be doing an additional test tomorrow regarding multibyte codepoints that should be recognised as illegal.

@Jafaral

Jafaral commented Aug 27, 2026

Copy link
Copy Markdown
Member Author

Thanks @brucerennie. Notice that I had to disable/skip the two utf8 tests you added a while back because they both fail since now built-in functions and string subscription is now utf8 aware. Those tests assumed byte/ascii semantic which is no longer true.

@brucerennie

Copy link
Copy Markdown
Collaborator

@Jafaral Not a problem. I am using the uft8_ovld.icn test as a basis to test all the relevant operators and functions. Once done, i will let you know the results as an alternative to your specific unicode tests.

@brucerennie

Copy link
Copy Markdown
Collaborator

@Jafaral The function ord(0 doesn't work on unicode codepoints if larger than 8 bits. As well there are a number of methods in the UTF8 class that would be useful for unicode, One tests for invalid sequences. I will continue checking.

@Jafaral

Jafaral commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

I kept ord() and char() byte-oriented for backward compatibility. They are used in in bit packing functions. They return different characters for upper ascii characters if the become unicode-aware. All other functions were backward compatible. So My PR introduces two new alternative functions that are unicode aware see here

This is something we can change, but at least that is what I went with for now.

A build with --enable-unicode lists "Unicode" in &features, and both
preprocessors predefine _UNICODE, so library code can use
$ifdef _UNICODE for what only a Unicode build needs.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Add stand/*.std files under descriptive names, a Makefile that skips
without Unicode, and include the suite in the top-level make Test list.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Share UTF-8 scan helpers and tag string literals in icont so []/* and
explicit unicode() can reuse one codepoint walk. bytes(s) is the
inverse: the same bytes with the tag cleared, so * and [] count bytes.

icont -b turns literal tagging off: every string literal stays a byte
string, as in a build without Unicode. unicon -b passes it to icont
(not to iconc, which never tags literals).

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Interpret move/tab/pos, find/match, and many in codepoints on uniqual
strings. many still cannot match non-ASCII cset members.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Reverse tagged strings by codepoint, retag substring assignment, and
opt in to tagged reads with open() mode i.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Land new coverage under tests/unicode with final names, adapt samples
to tagged s[i] extracts, and skip the class-library utf8 tests when
native Unicode is on.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
On a tagged string, generate and pick whole UTF-8 characters
instead of single bytes. Cache substring-assignment counts from
the spliced result so a mid-character trap cannot poison *.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Replace adjacent StrLoc/SetStrLen assignments with MakeStr where the
length is not derived from the same descriptor, so call sites share one
order. Leave Protect(alcstr) pairs and incremental length updates alone.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Uniqual UTF-8 literals make move(*fromStr) skip only one codepoint
and leave leftover bytes; CI pdflatex then dies on ellipsis/arrow/copyright.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Off-build StrLen is the historic dword. Tagged walks, uchar/uord/unicode(),
and open() mode i sit behind #ifdef UniconUnicode. Concat keeps a known
codepoint count when the other side is short and uncounted.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
strascii compares untagged ASCII string and cset operations with
Unicode compiled out versus in. strutf8 compares the same operations
on Arabic text, tagged versus the same bytes translated with -b.
Each program times one operation and reports CPU (&time) and wall
(gettimeofday).

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
malformed: a tagged string with a sequence cut short, or with a bad
continuation byte, walks each bad byte as one unit under !s, s[i],
s[i:j], ?s, reverse(), move(), find(), many() and match().
Concatenating untagged UTF-8 bytes onto a tagged string counts by
codepoint. find() and !s keep their place when a collection moves the
string mid-generation.

edges: an empty tagged string is not a variable, move() and tab()
re-read &subject when resumed, and reverse() recounts when malformed
bytes join into new characters.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
On a tagged subject, any, upto and bal walk by codepoint and report
codepoint positions, as many already does. A multi-byte character is
never a member of a 256-bit cset, so a continuation byte never matches
on its own. =s advances &pos by the characters matched and fails if
the match would end inside one, and assigning &pos converts the
position against the codepoint count.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
…entab

On a tagged string, widths, truncation and padding are in codepoints
and a pad string repeats character by character, so the result is
never cut inside a character. repl and trim keep the tag, and detab
and entab count one column per codepoint. A result is tagged only if
it holds a multi-byte character.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
When any argument of map is tagged, or s2 or s3 holds a non-ASCII
byte, s2 and s3 are paired by codepoint and must have equal codepoint
counts (error 208 otherwise). A later duplicate in s2 overrides an
earlier one, as with bytes. Malformed bytes map as themselves and
never equal a real codepoint. map(s) still maps only ASCII letters.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
strfuncs covers left, right, center, repl, trim, detab, entab and map
on tagged strings. scanfns covers any, upto, bal, many, find, match,
=s and &pos positions, the &pos default, and matches that would end
inside a multi-byte character.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
b_unistr and b_unicset sat under an #ifdef Unicode that nothing
defines, and no code refers to either struct. Tagged strings and
codepoint csets use other representations.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Static csets in data.r, rtt cset constants and iconc cset literals
spell out the block header as {T_Cset, n, ...}. Write it as
{CsetHdr(n), ...} instead, so the header can change in one place.
CsetHdr expands to the same text as before.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
In the Unicode build a cset block carries its block size, a tag word
and a sorted list of codepoint ranges above 255 after the 256-bit
bitmap, the way a large integer block carries its digits. Nothing
builds ranges or sets the tag yet, so no behaviour changes.

The collector reads the size from the block (bsizes[T_Cset] is 0).
alccset() still allocates a plain block with one range slot, and
alccset_n(n) allocates room for n ranges. cnv_tcset() clears the new
fields of its stack buffer, CsetHdr() writes the block size for static
csets, and the linker emits the new layout for cset literals.

Tagged string lengths already made Unicode-build icode unreadable by a
default iconx and the other way round, and now the cset layout does
too, so Unicode icode gets its own version, I12.U.31u, and accepts no
previous one. The default build is unchanged.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
In the Unicode build, cset(s) of a tagged string now gives a tagged
cset of its codepoints: 0-255 in the bitmap, the rest as sorted
ranges. A tagged cset converts back to a tagged UTF-8 string of its
members in ascending order, so *c, !c, ?c, string(c) and image(c)
work on characters, not bytes. A tagged cset holding more than half
of all codepoints images as ~unicode('...') of its non-members, which
evaluates back to the same cset. === and sort compare members and
ignore the tag, and unicode(c) returns the tagged view of a cset.
bytes(c) returns the untagged view of the same members, and is a
run-time error 205 when c has a member above 255.

Untagged csets and the default build are unchanged.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
In the Unicode build, ++, ** and -- on csets that hold codepoints
above 255, or are tagged, combine the bitmaps as before and merge the
range lists. The result is tagged if either operand is, and its ranges
stay sorted and non-adjacent, so === and hashing see one form for each
set of members.

~ of a tagged cset holds every codepoint up to U+10FFFF that the cset
lacks and stays tagged, so ~unicode(' ') matches any character but a
space on Unicode text.

Untagged byte csets, including their complement and &cset, take the
old path unchanged.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
In the Unicode build, any, upto, bal, many, trim and member test each
character of a tagged string by its codepoint: 0-255 in the cset's
bitmap, as before, and anything above that in its ranges. A one-byte
character, a stray byte included, still tests its byte value.

Before this a multi-byte character never matched, so
tab(many(cyrillic)) stopped at the first letter. member() on a cset
also tested a tagged string byte by byte; it now goes by character.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
In the Unicode build, a cset literal that holds at least one
well-formed multi-byte UTF-8 sequence is read by codepoint, stray
bytes by their byte value, and linked as a tagged cset with ranges for
anything above 255. So 'é' has one member, U+00E9, and 'абв' matches
Cyrillic text with any, upto and many.

A literal with no such sequence is a byte cset, as before. Because
the decision is made on the bytes, '\xc3\xa9' written with escapes
reads the same as a typed é, as string literals already do.

The translator marks such a literal with F_UniQualLit, as it does a
string literal, and the linker decodes only marked literals, so -b
keeps every cset literal a byte cset.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
cset_lit: which literals decode to codepoints and which stay bytes.
cset_conv: *c, !c, ?c, string, image, subscripts and unicode(c).
cset_ops: ++, ** and -- with ranges, and their laws.
cset_scan: many, upto, any, bal, trim and member on Cyrillic,
Latin-1 and emoji text, and an untagged subject tested by bytes.
cset_cmp: === and sort ignore the tag; tables and sets find a cset
by its members.
cset_gc: a cset with 128 ranges across collections.
cset_compl: ~ of tagged and byte csets on Arabic text: splitting
words, sun and moon letters, stripping the vowel marks, digits.
bytelits: translated with -b, Arabic string and cset literals stay
bytes while unicode() and concatenation with a tagged string still
give text.
bytes: bytes() of strings and csets, the round trip with unicode(),
using it to tell text from ASCII, and error 205 for a cset member
above 255.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
JSON and YAML text is UTF-8. jsonify_string() and the YAML quoting
helpers walk a string byte by byte to copy valid UTF-8 and escape the
rest, so with --enable-unicode a tagged string made them read a whole
character where they expected a byte, and ord() stopped with error
205 on any non-ASCII text (tojson("café"), and ulsp on a document
with an accented letter). They now scan bytes(s), reached with
proc("bytes", 0) so a program's own bytes() procedure does not
replace it. Builds without Unicode are unchanged.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Each thread keeps the last tagged string it counted or sought in, with
its codepoint count and the last codepoint and byte offset found.
uq_seek_pos() walks from the nearest of the start, that position and
the end, forward or back, so scanning a tagged &subject and loops over
s[i] or s[i:j] walk each character once instead of seeking from the
start. uq_count() remembers the count of a string that has none
cached. The collector moves the remembered string like map()'s cached
arguments.

tests/unicode/poscache reaches every character in several orders, on
Arabic text with and without a cached count and with stray bytes, and
scans across a collection. UTR 25 sections 9 and 10.3 describe the
cache and its numbers.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
Run make builtins in uni/ulsp. Hover and completion now know
unicode(), bytes(), uchar() and uord(), and go to definition lands on
the right line of the runtime functions that this series moved.

Signed-off-by: Jafar Al-Gharaibeh <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants