-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
String: some optimizations when ascii only
- v0.24.1
- 1.15.1
- 1.15.0
- 1.14.1
- 1.14.0
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.36.1
- 0.36.0
- 0.35.1
- 0.35.0
- 0.34.0
- 0.33.0
- 0.32.1
- 0.32.0
- 0.31.1
- 0.31.0
- 0.30.1
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.1
- 0.26.0
- 0.25.1
- 0.25.0
- 0.24.2
- 0.24.1
- 0.24.0
- 0.23.1
- 0.23.0
- 0.22.0
- 0.21.1
- 0.21.0
- 0.20.5
- 0.20.4
- 0.20.3
- 0.20.2
- 0.20.1
- 0.20.0
Ary Borenszweig
committed
Nov 22, 2016
1 parent
d599667
commit f5d54f4
Showing
1 changed file
with
56 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -875,10 +875,21 @@ class String | |
# "hEllO".downcase # => "hello" | ||
# ``` | ||
def downcase(options = Unicode::CaseOptions::None) | ||
String.build(bytesize) do |io| | ||
each_char do |char| | ||
char.downcase(options) do |res| | ||
io << res | ||
return self if empty? | ||
|
||
if ascii_only? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
asterite
Member
|
||
String.new(bytesize) do |buffer| | ||
bytesize.times do |i| | ||
buffer[i] = to_unsafe[i].unsafe_chr.downcase.ord.to_u8 | ||
end | ||
{@bytesize, @length} | ||
end | ||
else | ||
String.build(bytesize) do |io| | ||
each_char do |char| | ||
char.downcase(options) do |res| | ||
io << res | ||
end | ||
end | ||
end | ||
end | ||
|
@@ -891,10 +902,21 @@ class String | |
# "hEllO".upcase # => "HELLO" | ||
# ``` | ||
def upcase(options = Unicode::CaseOptions::None) | ||
String.build(bytesize) do |io| | ||
each_char do |char| | ||
char.upcase(options) do |res| | ||
io << res | ||
return self if empty? | ||
|
||
if ascii_only? | ||
String.new(bytesize) do |buffer| | ||
bytesize.times do |i| | ||
buffer[i] = to_unsafe[i].unsafe_chr.upcase.ord.to_u8 | ||
end | ||
{@bytesize, @length} | ||
end | ||
else | ||
String.build(bytesize) do |io| | ||
each_char do |char| | ||
char.upcase(options) do |res| | ||
io << res | ||
end | ||
end | ||
end | ||
end | ||
|
@@ -907,14 +929,27 @@ class String | |
# "hEllO".capitalize # => "Hello" | ||
# ``` | ||
def capitalize | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
return self if bytesize == 0 | ||
return self if empty? | ||
|
||
String.build(bytesize) do |io| | ||
each_char_with_index do |char, i| | ||
if i == 0 | ||
char.upcase { |c| io << c } | ||
else | ||
char.downcase { |c| io << c } | ||
if ascii_only? | ||
String.new(bytesize) do |buffer| | ||
bytesize.times do |i| | ||
if i == 0 | ||
buffer[i] = to_unsafe[i].unsafe_chr.upcase.ord.to_u8 | ||
else | ||
buffer[i] = to_unsafe[i].unsafe_chr.downcase.ord.to_u8 | ||
end | ||
end | ||
{@bytesize, @length} | ||
end | ||
else | ||
String.build(bytesize) do |io| | ||
each_char_with_index do |char, i| | ||
if i == 0 | ||
char.upcase { |c| io << c } | ||
else | ||
char.downcase { |c| io << c } | ||
end | ||
end | ||
end | ||
end | ||
|
@@ -933,7 +968,7 @@ class String | |
# | ||
# See also: `#chop` | ||
def chomp | ||
return self if bytesize == 0 | ||
return self if empty? | ||
|
||
case to_unsafe[bytesize - 1] | ||
when '\n' | ||
|
@@ -2725,6 +2760,8 @@ class String | |
# "eiffel_tower".camelcase # => "EiffelTower" | ||
# ``` | ||
def camelcase | ||
return self if empty? | ||
|
||
first = true | ||
last_is_underscore = false | ||
|
||
|
@@ -2752,6 +2789,8 @@ class String | |
# "racecar".reverse # => "racecar" | ||
# ``` | ||
def reverse | ||
return self if bytesize <= 1 | ||
|
||
if ascii_only? | ||
String.new(bytesize) do |buffer| | ||
bytesize.times do |i| | ||
|
@@ -2844,7 +2883,7 @@ class String | |
# "***".succ # => "**+" | ||
# ``` | ||
def succ | ||
return self if bytesize == 0 | ||
return self if empty? | ||
|
||
chars = self.chars | ||
|
||
|
Maybe document the assumption that the case options won't have an effect on ascii characters?