Skip to content

Commit

Permalink
Showing 3 changed files with 23 additions and 14 deletions.
32 changes: 20 additions & 12 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
@@ -1391,18 +1391,26 @@ describe "String" do
s.bytesize.should eq(3)
end

it "tr" do
"bla".tr("a", "h").should eq("blh")
"bla".tr("a", "").should eq("bl⊙")
"bl⊙a".tr("", "a").should eq("blaa")
"bl⊙a".tr("", "").should eq("blⓧa")
"bl⊙a⊙asdfd⊙dsfsdf⊙⊙⊙".tr("a⊙", "ⓧt").should eq("bltⓧtⓧsdfdtdsfsdfttt")
"hello".tr("aeiou", "*").should eq("h*ll*")
"hello".tr("el", "ip").should eq("hippo")
"Lisp".tr("Lisp", "Crys").should eq("Crys")
"hello".tr("helo", "1212").should eq("12112")
"this".tr("this", "").should eq("ⓧⓧⓧⓧ")
"über".tr("ü", "u").should eq("uber")
describe "tr" do
it "translates" do
"bla".tr("a", "h").should eq("blh")
"bla".tr("a", "").should eq("bl⊙")
"bl⊙a".tr("", "a").should eq("blaa")
"bl⊙a".tr("", "").should eq("blⓧa")
"bl⊙a⊙asdfd⊙dsfsdf⊙⊙⊙".tr("a⊙", "ⓧt").should eq("bltⓧtⓧsdfdtdsfsdfttt")
"hello".tr("aeiou", "*").should eq("h*ll*")
"hello".tr("el", "ip").should eq("hippo")
"Lisp".tr("Lisp", "Crys").should eq("Crys")
"hello".tr("helo", "1212").should eq("12112")
"this".tr("this", "").should eq("ⓧⓧⓧⓧ")
"über".tr("ü", "u").should eq("uber")
end

context "given no replacement characters" do
it "acts as #delete" do
"foo".tr("o", "").should eq("foo".delete("o"))
end
end
end

describe "compare" do
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/const.cr
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ require "./codegen"

class Crystal::CodeGenVisitor
# The special constants ARGC_UNSAFE and ARGV_UNSAFE need to be initialized
# as soon as the program starts, because we have access to argc and arv
# as soon as the program starts, because we have access to argc and argv
# in the main function
def initialize_argv_and_argc
{"ARGC_UNSAFE", "ARGV_UNSAFE"}.each do |name|
3 changes: 2 additions & 1 deletion src/string.cr
Original file line number Diff line number Diff line change
@@ -1179,14 +1179,15 @@ class String

# Returns a new string _tr_anslating characters using *from* and *to* as a
# map. If *to* is shorter than *from*, the last character in *to* is used for
# the rest.
# the rest. If *to* is empty, this acts like `String#delete`.
#
# ```
# "aabbcc".tr("abc", "xyz") # => "xxyyzz"
# "aabbcc".tr("abc", "x") # => "xxxxxx"
# "aabbcc".tr("a", "xyz") # => "xxbbcc"
# ```
def tr(from : String, to : String)
return delete(from) if to.empty?
multi = nil
table = StaticArray(Int32, 256).new(-1)
reader = Char::Reader.new(to)

0 comments on commit 356cf4d

Please sign in to comment.