summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-string-ext')
-rw-r--r--mrbgems/mruby-string-ext/mrbgem.rake1
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb22
-rw-r--r--mrbgems/mruby-string-ext/src/string.c63
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb62
4 files changed, 120 insertions, 28 deletions
diff --git a/mrbgems/mruby-string-ext/mrbgem.rake b/mrbgems/mruby-string-ext/mrbgem.rake
index 4a3369998..688589933 100644
--- a/mrbgems/mruby-string-ext/mrbgem.rake
+++ b/mrbgems/mruby-string-ext/mrbgem.rake
@@ -1,4 +1,5 @@
MRuby::Gem::Specification.new('mruby-string-ext') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
+ spec.summary = 'extensional String class'
end
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index 005438b38..a517aa209 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -49,4 +49,26 @@ class String
def casecmp(str)
self.downcase <=> str.downcase
end
+
+ def partition(sep)
+ raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
+ n = index(sep)
+ unless n.nil?
+ m = n + sep.size
+ [ slice(0, n), sep, slice(m, size - m) ]
+ else
+ [ self, "", "" ]
+ end
+ end
+
+ def rpartition(sep)
+ raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
+ n = rindex(sep)
+ unless n.nil?
+ m = n + sep.size
+ [ slice(0, n), sep, slice(m, size - m) ]
+ else
+ [ "", "", self ]
+ end
+ end
end
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index edebcecbc..3ec23c51f 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -1,7 +1,7 @@
-#include "mruby.h"
-#include "mruby/string.h"
#include <ctype.h>
#include <string.h>
+#include "mruby.h"
+#include "mruby/string.h"
static mrb_value
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
@@ -33,8 +33,8 @@ mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
struct RString *s = mrb_str_ptr(str);
mrb_str_modify(mrb, s);
- p = s->ptr;
- pend = s->ptr + s->len;
+ p = RSTRING_PTR(str);
+ pend = p + RSTRING_LEN(str);
while (p < pend) {
if (ISUPPER(*p)) {
*p = TOLOWER(*p);
@@ -107,22 +107,27 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self)
* # returns true if one of the prefixes matches.
* "hello".start_with?("heaven", "hell") #=> true
* "hello".start_with?("heaven", "paradise") #=> false
+ * "h".start_with?("heaven", "hell") #=> false
*/
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
{
- mrb_value *argv;
+ mrb_value *argv, sub;
int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
- size_t len_l, len_r, len_cmp;
+ size_t len_l, len_r;
+ int ai = mrb_gc_arena_save(mrb);
+ sub = mrb_string_type(mrb, argv[i]);
+ mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
- len_r = RSTRING_LEN(argv[i]);
- len_cmp = (len_l > len_r) ? len_r : len_l;
- if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_cmp) == 0) {
- return mrb_true_value();
- }
+ len_r = RSTRING_LEN(sub);
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}
@@ -136,24 +141,40 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_str_end_with(mrb_state *mrb, mrb_value self)
{
- mrb_value *argv;
+ mrb_value *argv, sub;
int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
- size_t len_l, len_r, len_cmp;
+ size_t len_l, len_r;
+ int ai = mrb_gc_arena_save(mrb);
+ sub = mrb_string_type(mrb, argv[i]);
+ mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
- len_r = RSTRING_LEN(argv[i]);
- len_cmp = (len_l > len_r) ? len_r : len_l;
- if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp),
- RSTRING_PTR(argv[i]) + (len_r - len_cmp),
- len_cmp) == 0) {
- return mrb_true_value();
- }
+ len_r = RSTRING_LEN(sub);
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
+ RSTRING_PTR(sub),
+ len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}
+static mrb_value
+mrb_str_hex(mrb_state *mrb, mrb_value self)
+{
+ return mrb_str_to_inum(mrb, self, 16, FALSE);
+}
+
+static mrb_value
+mrb_str_oct(mrb_state *mrb, mrb_value self)
+{
+ return mrb_str_to_inum(mrb, self, 8, FALSE);
+}
+
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
@@ -167,6 +188,8 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, s, "<<", mrb_str_concat2, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "start_with?", mrb_str_start_with, MRB_ARGS_REST());
mrb_define_method(mrb, s, "end_with?", mrb_str_end_with, MRB_ARGS_REST());
+ mrb_define_method(mrb, s, "hex", mrb_str_hex, MRB_ARGS_NONE());
+ mrb_define_method(mrb, s, "oct", mrb_str_oct, MRB_ARGS_NONE());
}
void
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 2bb32cef3..90bb43c65 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -20,8 +20,7 @@ assert('String#dump') do
end
assert('String#strip') do
- s = " abc "
- s.strip
+ s = " abc "
"".strip == "" and " \t\r\n\f\v".strip == "" and
"\0a\0".strip == "\0a" and
"abc".strip == "abc" and
@@ -74,8 +73,8 @@ assert('String#rstrip!') do
end
assert('String#swapcase') do
- assert_equal "Hello".swapcase, "hELLO"
- assert_equal "cYbEr_PuNk11".swapcase, "CyBeR_pUnK11"
+ assert_equal "hELLO", "Hello".swapcase
+ assert_equal "CyBeR_pUnK11", "cYbEr_PuNk11".swapcase
end
assert('String#swapcase!') do
@@ -95,18 +94,65 @@ assert('String#concat') do
end
assert('String#casecmp') do
- assert_equal "abcdef".casecmp("abcde"), 1
- assert_equal "aBcDeF".casecmp("abcdef"), 0
- assert_equal "abcdef".casecmp("abcdefg"),-1
- assert_equal "abcdef".casecmp("ABCDEF"), 0
+ assert_equal 1, "abcdef".casecmp("abcde")
+ assert_equal 0, "aBcDeF".casecmp("abcdef")
+ assert_equal(-1, "abcdef".casecmp("abcdefg"))
+ assert_equal 0, "abcdef".casecmp("ABCDEF")
end
assert('String#start_with?') do
assert_true "hello".start_with?("heaven", "hell")
assert_true !"hello".start_with?("heaven", "paradise")
+ assert_true !"h".start_with?("heaven", "hell")
+ assert_raise TypeError do "hello".start_with?(true) end
end
assert('String#end_with?') do
assert_true "string".end_with?("ing", "mng")
assert_true !"string".end_with?("str", "tri")
+ assert_true !"ng".end_with?("ing", "mng")
+ assert_raise TypeError do "hello".end_with?(true) end
+end
+
+assert('String#partition') do
+ assert_equal ["a", "x", "axa"], "axaxa".partition("x")
+ assert_equal ["aaaaa", "", ""], "aaaaa".partition("x")
+ assert_equal ["", "", "aaaaa"], "aaaaa".partition("")
+ assert_equal ["", "a", "aaaa"], "aaaaa".partition("a")
+ assert_equal ["aaaa", "b", ""], "aaaab".partition("b")
+ assert_equal ["", "b", "aaaa"], "baaaa".partition("b")
+ assert_equal ["", "", ""], "".partition("a")
+end
+
+assert('String#rpartition') do
+ assert_equal ["axa", "x", "a"], "axaxa".rpartition("x")
+ assert_equal ["", "", "aaaaa"], "aaaaa".rpartition("x")
+ assert_equal ["aaaaa", "", ""], "aaaaa".rpartition("")
+ assert_equal ["aaaa", "a", ""], "aaaaa".rpartition("a")
+ assert_equal ["aaaa", "b", ""], "aaaab".rpartition("b")
+ assert_equal ["", "b", "aaaa"], "baaaa".rpartition("b")
+ assert_equal ["", "", ""], "".rpartition("a")
+end
+
+assert('String#hex') do
+ assert_equal 16, "10".hex
+ assert_equal 255, "ff".hex
+ assert_equal 16, "0x10".hex
+ assert_equal (-16), "-0x10".hex
+ assert_equal 0, "xyz".hex
+ assert_equal 16, "10z".hex
+ assert_equal 16, "1_0".hex
+ assert_equal 0, "".hex
+end
+
+assert('String#oct') do
+ assert_equal 8, "10".oct
+ assert_equal 7, "7".oct
+ assert_equal 0, "8".oct
+ assert_equal 0, "9".oct
+ assert_equal 0, "xyz".oct
+ assert_equal 8, "10z".oct
+ assert_equal 8, "1_0".oct
+ assert_equal 8, "010".oct
+ assert_equal (-8), "-10".oct
end