Skip to content

Commit

Permalink
Showing 5 changed files with 38 additions and 10 deletions.
8 changes: 8 additions & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
@@ -21,6 +21,14 @@ describe "Char" do
assert { 'ぃ'.pred.should eq('あ') }
end

describe "+" do
assert { ('a' + 2).should eq('c') }
end

describe "-" do
assert { ('c' - 2).should eq('a') }
end

describe "uppercase?" do
assert { 'a'.uppercase?.should be_false }
assert { 'A'.uppercase?.should be_true }
4 changes: 2 additions & 2 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
@@ -176,7 +176,7 @@ describe "IO::Buffered" do
s = String.build do |str|
900.times do
10.times do |i|
str << ('a'.ord + i).chr
str << ('a' + i)
end
end
end
@@ -197,7 +197,7 @@ describe "IO::Buffered" do
s = String.build do |str|
900.times do
10.times do |i|
str << ('a'.ord + i).chr
str << ('a' + i)
end
end
end
8 changes: 4 additions & 4 deletions spec/std/slice_spec.cr
Original file line number Diff line number Diff line change
@@ -178,13 +178,13 @@ describe "Slice" do
it "handles intersecting ranges" do
# Test with ranges offset by 0 to 8 bytes
(0..8).each do |offset|
buf = Slice.new(16) { |i| ('a'.ord + i).chr }
buf = Slice.new(16) { |i| 'a' + i }
dst = buf[0, 8]
src = buf[offset, 8]

src.move_to(dst)

result = (0..7).map { |i| ('a'.ord + i + offset).chr }
result = (0..7).map { |i| 'a' + i + offset }
dst.should eq(Slice.new(result.to_unsafe, result.size))
end
end
@@ -217,13 +217,13 @@ describe "Slice" do
it "handles intersecting ranges" do
# Test with ranges offset by 0 to 8 bytes
(0..8).each do |offset|
buf = Slice.new(16) { |i| ('a'.ord + i).chr }
buf = Slice.new(16) { |i| 'a' + i }
dst = buf[0, 8]
src = buf[offset, 8]

dst.move_from(src)

result = (0..7).map { |i| ('a'.ord + i + offset).chr }
result = (0..7).map { |i| 'a' + i + offset }
dst.should eq(Slice.new(result.to_unsafe, result.size))
end
end
20 changes: 20 additions & 0 deletions src/char.cr
Original file line number Diff line number Diff line change
@@ -89,6 +89,26 @@ struct Char
end
end

# Returns a char that has this char's codepoint plus *other*.
#
# ```
# 'a' + 1 # => 'b'
# 'a' + 2 # => 'c'
# ```
def +(other : Int) : Char
(ord + other).chr
end

# Returns a char that has this char's codepoint minus *other*.
#
# ```
# 'c' - 1 # => 'b'
# 'c' - 2 # => 'a'
# ```
def -(other : Int) : Char
(ord - other).chr
end

# Implements the comparison operator.
#
# ```
8 changes: 4 additions & 4 deletions src/string.cr
Original file line number Diff line number Diff line change
@@ -1232,8 +1232,8 @@ class String
# block and replaced by its return value.
#
# ```
# "hello".sub { |x| (x.ord + 1).chr } # => "iello"
# "hello".sub { "hi" } # => "hiello"
# "hello".sub { |char| char + 1 } # => "iello"
# "hello".sub { "hi" } # => "hiello"
# ```
def sub(&block : Char -> _)
return self if empty?
@@ -1587,8 +1587,8 @@ class String
# is replaced by the block's return value.
#
# ```
# "hello".gsub { |x| (x.ord + 1).chr } # => "ifmmp"
# "hello".gsub { "hi" } # => "hihihihihi"
# "hello".gsub { |char| char + 1 } # => "ifmmp"
# "hello".gsub { "hi" } # => "hihihihihi"
# ```
def gsub(&block : Char -> _)
String.build(bytesize) do |buffer|

0 comments on commit 3430284

Please sign in to comment.