diff --git a/spec/compiler/codegen/fun_spec.cr b/spec/compiler/codegen/fun_spec.cr index bf2bd400e106c3f3bf0e780e7e232f06e7e2dd7b..a71b52fab07317fc97d721b0bfd16c36eae6f19c 100644 --- a/spec/compiler/codegen/fun_spec.cr +++ b/spec/compiler/codegen/fun_spec.cr @@ -86,7 +86,7 @@ describe "Code gen: fun" do ").to_i.should eq(1) end - pending "codegens fun that accepts a union and is called with a single type" do + it "codegens fun that accepts a union and is called with a single type" do run(" f = ->(x : Int32 | Float64) { x + 1 } f.call(1).to_i diff --git a/spec/compiler/codegen/union_type_spec.cr b/spec/compiler/codegen/union_type_spec.cr index 2c7939eed819f2c21d49cac794e256a9d95ef644..8728603d528a361b8ac099648782dce3a044ceef 100644 --- a/spec/compiler/codegen/union_type_spec.cr +++ b/spec/compiler/codegen/union_type_spec.cr @@ -64,7 +64,7 @@ describe "Code gen: union type" do ").to_i.should eq(1) end - pending "assigns union to union" do + it "assigns union to union" do run(" require \"prelude\" diff --git a/spec/std/function_spec.cr b/spec/std/function_spec.cr index 0193a4907dc1b8f8c8e6417d2c3f60eadefe01a2..aa83a7214668da207ea36a2bca1938ba466b54a3 100644 --- a/spec/std/function_spec.cr +++ b/spec/std/function_spec.cr @@ -17,13 +17,13 @@ describe "Function" do str.to_s.should eq("#<(Int32 -> Float64):0x#{f.pointer.address.to_s(16)}:closure>") end - pending "does to_s" do + it "does to_s" do str = StringIO.new f = ->(x : Int32) { x.to_f } f.to_s.should eq("#<(Int32 -> Float64):0x#{f.pointer.address.to_s(16)}>") end - pending "does to_s when closured" do + it "does to_s when closured" do str = StringIO.new a = 1.5 f = ->(x : Int32) { x + a } diff --git a/spec/std/string_spec.cr b/spec/std/string_spec.cr index 7936648c792434bc9c7c69b1f7962acc38ba3de2..04afb4e311a1a7f608a4d2cf8283d2c1cfd76375 100755 --- a/spec/std/string_spec.cr +++ b/spec/std/string_spec.cr @@ -44,7 +44,7 @@ describe "String" do "1234123412341234".to_i64.should eq(1234123412341234_i64) end - pending "does to_u64" do + it "does to_u64" do "9223372036854775808".to_u64.should eq(9223372036854775808_u64) end @@ -368,11 +368,15 @@ describe "String" do "\033a"[1].should eq('a') end - pending "escapes with hex" do - "\x12"[0].should eq(1 * 16 + 2) - "\xA"[0].should eq(10) - "\xAB"[0].should eq(10 * 16 + 11) - "\xAB1"[1].should eq('1'.ord) + it "escapes with hex" do + "\x12".codepoint_at(0).should eq(1 * 16 + 2) + "\xA".codepoint_at(0).should eq(10) + "\xAB".codepoint_at(0).should eq(10 * 16 + 11) + "\xAB1".codepoint_at(1).should eq('1'.ord) + end + + it "does char_at" do + "ã„ãŸã ãã¾ã™".char_at(2).should eq('ã ') end it "allows creating a string with zeros" do diff --git a/src/string.cr b/src/string.cr index 42a6a674a20788a1fcf950dcc0f8cf5ed0f462ab..7eb64efb6418088261f7691400b86fef2397aced 100644 --- a/src/string.cr +++ b/src/string.cr @@ -144,6 +144,22 @@ class String end end + def codepoint_at(index) + char_at(index).ord + end + + def char_at(index) + i = 0 + each_char do |char| + if i == index + return char + end + i += 1 + end + + raise IndexOutOfBounds.new + end + def downcase String.new_with_length(length) do |buffer| length.times do |i|