Skip to content

Commit 63e4432

Browse files
committed
Use Pointer::Appender instead of buffer mutation
1 parent 447615c commit 63e4432

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/enum.cr

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,19 +511,23 @@ abstract struct Enum
511511
return parse_slow?(string) if string.bytesize > 100
512512

513513
{% begin %}
514-
buffer = uninitialized UInt8[100]
515-
buffer_index = 0i64
516-
string.each_char do |char|
514+
# FIXME: There is no `StringLiteral#bytesize` or any other adequate means
515+
# to figure out how much space we actually need. Maybe some regex could
516+
# work. For now just play it safe.
517+
# FIXME: We might want to establish some upper limit in case a member name
518+
# is exorbitantly long.
519+
buffer = uninitialized UInt8[{{ @type.constants.map(&.size).sort.last * 4 + 1 }}]
520+
max_size = {{ @type.constants.map(&.size).sort.last }}
521+
appender = buffer.to_slice.to_unsafe.appender
522+
string.each_char_with_index do |char, index|
523+
return nil if index >= max_size
517524
next if char == '-' || char == '_'
518-
char.downcase do |lower|
519-
lower.each_byte do |byte|
520-
buffer[buffer_index] = byte
521-
buffer_index &+= 1
522-
end
525+
char.downcase &.each_byte do |byte|
526+
appender << byte
523527
end
524528
end
525529

526-
case buffer.to_slice[0...buffer_index]
530+
case appender.to_slice
527531
# Temporarily map all constants to their normalized value in order to
528532
# avoid duplicates in the `case` conditions.
529533
# `FOO` and `Foo` members would both generate `when "foo"` which creates a compile time error.

0 commit comments

Comments
 (0)