diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-12-30 21:09:49 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-12-30 21:09:49 -0800 |
| commit | c845798ba40d16b94d421b7a128497c009726e75 (patch) | |
| tree | 5912cd895c741187bb651da226c767e3903083c9 /mrbgems/mruby-string-ext/mrblib/string.rb | |
| parent | 70d6d04ce9d447f1c0e9825a0ef9babbfadb5994 (diff) | |
| parent | 120a02c15dfe91451f01f1283ec53c207f2d39be (diff) | |
| download | mruby-c845798ba40d16b94d421b7a128497c009726e75.tar.gz mruby-c845798ba40d16b94d421b7a128497c009726e75.zip | |
Merge pull request #1625 from iij/pr-string-partition
add String#partition and String#rpartition.
Diffstat (limited to 'mrbgems/mruby-string-ext/mrblib/string.rb')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 22 |
1 files changed, 22 insertions, 0 deletions
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 |
