Skip to content

Commit f5d54f4

Browse files
author
Ary Borenszweig
committedNov 22, 2016
String: some optimizations when ascii only
1 parent d599667 commit f5d54f4

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed
 

Diff for: ‎src/string.cr

+56-17
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,21 @@ class String
875875
# "hEllO".downcase # => "hello"
876876
# ```
877877
def downcase(options = Unicode::CaseOptions::None)
878-
String.build(bytesize) do |io|
879-
each_char do |char|
880-
char.downcase(options) do |res|
881-
io << res
878+
return self if empty?
879+
880+
if ascii_only?
Has comments. Original line has comments.
881+
String.new(bytesize) do |buffer|
882+
bytesize.times do |i|
883+
buffer[i] = to_unsafe[i].unsafe_chr.downcase.ord.to_u8
884+
end
885+
{@bytesize, @length}
886+
end
887+
else
888+
String.build(bytesize) do |io|
889+
each_char do |char|
890+
char.downcase(options) do |res|
891+
io << res
892+
end
882893
end
883894
end
884895
end
@@ -891,10 +902,21 @@ class String
891902
# "hEllO".upcase # => "HELLO"
892903
# ```
893904
def upcase(options = Unicode::CaseOptions::None)
894-
String.build(bytesize) do |io|
895-
each_char do |char|
896-
char.upcase(options) do |res|
897-
io << res
905+
return self if empty?
906+
907+
if ascii_only?
908+
String.new(bytesize) do |buffer|
909+
bytesize.times do |i|
910+
buffer[i] = to_unsafe[i].unsafe_chr.upcase.ord.to_u8
911+
end
912+
{@bytesize, @length}
913+
end
914+
else
915+
String.build(bytesize) do |io|
916+
each_char do |char|
917+
char.upcase(options) do |res|
918+
io << res
919+
end
898920
end
899921
end
900922
end
@@ -907,14 +929,27 @@ class String
907929
# "hEllO".capitalize # => "Hello"
908930
# ```
909931
def capitalize
Has comments. Original line has comments.
910-
return self if bytesize == 0
932+
return self if empty?
911933

912-
String.build(bytesize) do |io|
913-
each_char_with_index do |char, i|
914-
if i == 0
915-
char.upcase { |c| io << c }
916-
else
917-
char.downcase { |c| io << c }
934+
if ascii_only?
935+
String.new(bytesize) do |buffer|
936+
bytesize.times do |i|
937+
if i == 0
938+
buffer[i] = to_unsafe[i].unsafe_chr.upcase.ord.to_u8
939+
else
940+
buffer[i] = to_unsafe[i].unsafe_chr.downcase.ord.to_u8
941+
end
942+
end
943+
{@bytesize, @length}
944+
end
945+
else
946+
String.build(bytesize) do |io|
947+
each_char_with_index do |char, i|
948+
if i == 0
949+
char.upcase { |c| io << c }
950+
else
951+
char.downcase { |c| io << c }
952+
end
918953
end
919954
end
920955
end
@@ -933,7 +968,7 @@ class String
933968
#
934969
# See also: `#chop`
935970
def chomp
936-
return self if bytesize == 0
971+
return self if empty?
937972

938973
case to_unsafe[bytesize - 1]
939974
when '\n'
@@ -2725,6 +2760,8 @@ class String
27252760
# "eiffel_tower".camelcase # => "EiffelTower"
27262761
# ```
27272762
def camelcase
2763+
return self if empty?
2764+
27282765
first = true
27292766
last_is_underscore = false
27302767

@@ -2752,6 +2789,8 @@ class String
27522789
# "racecar".reverse # => "racecar"
27532790
# ```
27542791
def reverse
2792+
return self if bytesize <= 1
2793+
27552794
if ascii_only?
27562795
String.new(bytesize) do |buffer|
27572796
bytesize.times do |i|
@@ -2844,7 +2883,7 @@ class String
28442883
# "***".succ # => "**+"
28452884
# ```
28462885
def succ
2847-
return self if bytesize == 0
2886+
return self if empty?
28482887

28492888
chars = self.chars
28502889

0 commit comments

Comments
 (0)
Please sign in to comment.