File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -511,19 +511,23 @@ abstract struct Enum
511
511
return parse_slow?(string) if string.bytesize > 100
512
512
513
513
{% begin % }
514
- buffer = uninitialized UInt8 [100 ]
515
- buffer_index = 0 i64
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
517
524
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
523
527
end
524
528
end
525
529
526
- case buffer .to_slice[ 0 ...buffer_index]
530
+ case appender .to_slice
527
531
# Temporarily map all constants to their normalized value in order to
528
532
# avoid duplicates in the `case` conditions.
529
533
# `FOO` and `Foo` members would both generate `when "foo"` which creates a compile time error.
You can’t perform that action at this time.
0 commit comments