Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/unicase.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,19 @@ mrb_uni_case_unfold(uint32_t cp, uint32_t *out, int max)
out[n++] = folded - 32;
}

/* A non-ASCII source folding into ASCII (U+017F into 's') is in a table and
is found by these scans like any other. */
/* A non-ASCII source folding into ASCII (U+017F into 's') is the only thing
an ASCII source can find in a table, and the generator lists those beside
the tables, so the list is the whole answer for one. What that saves is
the two full scans below, which an ASCII source would otherwise make to
collect the entries the list already holds. Most patterns are ASCII and
nothing else, and this is what /i costs them. */
if (cp < 128) {
#define X(src, to) if (folded == (to) && n < max) out[n++] = (src);
UNI_FOLD_TO_ASCII
#undef X
return n;
}

n = unfold_from(&case_tables[MRB_CASE_KIND_FOLD], cp, folded, out, max, n);
n = unfold_from(&case_tables[MRB_CASE_KIND_LOWER], cp, folded, out, max, n);
return n;
Expand Down Expand Up @@ -359,6 +370,17 @@ void
mrb_uni_case_unfold_range(uint32_t lo, uint32_t hi,
void (*add)(void *, uint32_t, uint32_t), void *user)
{
/* An ASCII fold has the listed sources and no others, for the reason
mrb_uni_case_unfold() gives, so the ASCII part of the span is answered
from the list and the walks below are handed what is left of it. A class
of nothing but ASCII, which is most classes, reaches neither walk. */
if (lo < 128) {
#define X(src, to) if (lo <= (to) && (to) <= hi) add(user, (src), (src));
UNI_FOLD_TO_ASCII
#undef X
if (hi < 128) return;
lo = 128;
}
unfold_range_of(&case_tables[MRB_CASE_KIND_FOLD], FALSE, lo, hi, add, user);
unfold_range_of(&case_tables[MRB_CASE_KIND_LOWER], TRUE, lo, hi, add, user);
}
Expand Down
8 changes: 8 additions & 0 deletions src/unicase.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,12 @@ static const uint8_t uni_fold_multi[] = {
#define UNI_FOLD_MIN 0x000B5
#define UNI_FOLD_MAX 0x0FB17

/* The sources above ASCII whose simple folding is an ASCII character, as
a list the caller expands with an X of its own. ASCII itself is in no
table, so unfolding an ASCII character is these and the ASCII rules,
and no table is read at all. */
#define UNI_FOLD_TO_ASCII \
X(0x0017F, 0x00073) /* to 's' */ \
X(0x0212A, 0x0006B) /* to 'k' */

#endif /* MRB_UNICASE_H */
22 changes: 22 additions & 0 deletions tools/gen_unicase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ def multis_of(map)
# to TABLES and left out here would be one the buffer is too small for.
widest = TABLES.flat_map { |_, map, _| map.values.map(&:bytesize) }.max

# The sources above ASCII whose simple folding is an ASCII character, read the
# way the lookup reads folding: the difference first, and the lowercase mapping
# under it for the sources the difference passes over. ASCII is in no table, so
# these are the whole of what unfolding an ASCII character can find there, and
# emitting them lets that answer be given without a table at all. Measured over
# the tables rather than listed beside them for the reason `widest` is.
fold_to_ascii = (fold_diff.keys | lower.keys).sort.map do |cp|
to = (fold_diff[cp] || lower[cp]).unpack("U*")
next nil unless to.size == 1 && to[0] < 0x80
[cp, to[0]]
end.compact

# ------------------------------------------------------------------- emit

def hex(cp)
Expand Down Expand Up @@ -273,6 +285,16 @@ def hex(cp)
out.puts "#define UNI_#{up}_MAX #{hex(hi)}"
end

out.puts
out.puts "/* The sources above ASCII whose simple folding is an ASCII character, as"
out.puts " a list the caller expands with an X of its own. ASCII itself is in no"
out.puts " table, so unfolding an ASCII character is these and the ASCII rules,"
out.puts " and no table is read at all. */"
out.puts "#define UNI_FOLD_TO_ASCII" +
fold_to_ascii.map { |cp, to|
" \\\n X(#{hex(cp)}, #{hex(to)}) /* to '#{to.chr}' */"
}.join

out.puts
out.puts "#endif /* MRB_UNICASE_H */"
end
Expand Down
Loading