@@ -875,10 +875,21 @@ class String
875
875
# "hEllO".downcase # => "hello"
876
876
# ```
877
877
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
882
893
end
883
894
end
884
895
end
@@ -891,10 +902,21 @@ class String
891
902
# "hEllO".upcase # => "HELLO"
892
903
# ```
893
904
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
898
920
end
899
921
end
900
922
end
@@ -907,14 +929,27 @@ class String
907
929
# "hEllO".capitalize # => "Hello"
908
930
# ```
909
931
def capitalize
Has comments. Original line has comments. 910
- return self if bytesize == 0
932
+ return self if empty?
911
933
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
918
953
end
919
954
end
920
955
end
@@ -933,7 +968,7 @@ class String
933
968
#
934
969
# See also: `#chop`
935
970
def chomp
936
- return self if bytesize == 0
971
+ return self if empty?
937
972
938
973
case to_unsafe[bytesize - 1 ]
939
974
when '\n'
@@ -2725,6 +2760,8 @@ class String
2725
2760
# "eiffel_tower".camelcase # => "EiffelTower"
2726
2761
# ```
2727
2762
def camelcase
2763
+ return self if empty?
2764
+
2728
2765
first = true
2729
2766
last_is_underscore = false
2730
2767
@@ -2752,6 +2789,8 @@ class String
2752
2789
# "racecar".reverse # => "racecar"
2753
2790
# ```
2754
2791
def reverse
2792
+ return self if bytesize <= 1
2793
+
2755
2794
if ascii_only?
2756
2795
String .new(bytesize) do |buffer |
2757
2796
bytesize.times do |i |
@@ -2844,7 +2883,7 @@ class String
2844
2883
# "***".succ # => "**+"
2845
2884
# ```
2846
2885
def succ
2847
- return self if bytesize == 0
2886
+ return self if empty?
2848
2887
2849
2888
chars = self .chars
2850
2889