summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/mrblib/string.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-string-ext/mrblib/string.rb')
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb157
1 files changed, 148 insertions, 9 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index b8cb93199..c699e13b7 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -1,6 +1,25 @@
class String
##
+ # call-seq:
+ # String.try_convert(obj) -> string or nil
+ #
+ # Try to convert <i>obj</i> into a String, using to_str method.
+ # Returns converted string or nil if <i>obj</i> cannot be converted
+ # for any reason.
+ #
+ # String.try_convert("str") #=> "str"
+ # String.try_convert(/re/) #=> nil
+ #
+ def self.try_convert(obj)
+ if obj.respond_to?(:to_str)
+ obj.to_str
+ else
+ nil
+ end
+ end
+
+ ##
# call-seq:
# string.clear -> string
#
@@ -164,9 +183,9 @@ class String
# string #=> "thsa sting"
#
def slice!(arg1, arg2=nil)
- raise "wrong number of arguments (for 1..2)" if arg1 == nil && arg2 == nil
+ raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
- if arg1 != nil && arg2 != nil
+ if !arg1.nil? && !arg2.nil?
idx = arg1
idx += self.size if arg1 < 0
if idx >= 0 && idx <= self.size && arg2 > 0
@@ -181,6 +200,7 @@ class String
ed = arg1.end
beg += self.size if beg < 0
ed += self.size if ed < 0
+ ed -= 1 if arg1.exclude_end?
validated = true
elsif arg1.kind_of?(String)
validated = true
@@ -195,25 +215,144 @@ class String
return nil
end
end
- unless str == nil || str == ""
- if arg1 != nil && arg2 !=nil
+ unless str.nil? || str == ""
+ if !arg1.nil? && !arg2.nil?
idx = arg1 >= 0 ? arg1 : self.size+arg1
- str2 = self[0...idx] + self[idx+arg2..-1]
+ str2 = self[0...idx] + self[idx+arg2..-1].to_s
else
if arg1.kind_of?(Range)
idx = beg >= 0 ? beg : self.size+beg
idx2 = ed>= 0 ? ed : self.size+ed
- str2 = self[0...idx] + self[idx2+1..-1]
+ str2 = self[0...idx] + self[idx2+1..-1].to_s
elsif arg1.kind_of?(String)
idx = self.index(arg1)
- str2 = self[0...idx] + self[idx+arg1.size..-1] unless idx == nil
+ str2 = self[0...idx] + self[idx+arg1.size..-1] unless idx.nil?
else
idx = arg1 >= 0 ? arg1 : self.size+arg1
- str2 = self[0...idx] + self[idx+1..-1]
+ str2 = self[0...idx] + self[idx+1..-1].to_s
end
end
- self.replace(str2) unless str2 == nil
+ self.replace(str2) unless str2.nil?
end
str
end
+
+ ##
+ # call-seq:
+ # str.insert(index, other_str) -> str
+ #
+ # Inserts <i>other_str</i> before the character at the given
+ # <i>index</i>, modifying <i>str</i>. Negative indices count from the
+ # end of the string, and insert <em>after</em> the given character.
+ # The intent is insert <i>aString</i> so that it starts at the given
+ # <i>index</i>.
+ #
+ # "abcd".insert(0, 'X') #=> "Xabcd"
+ # "abcd".insert(3, 'X') #=> "abcXd"
+ # "abcd".insert(4, 'X') #=> "abcdX"
+ # "abcd".insert(-3, 'X') #=> "abXcd"
+ # "abcd".insert(-1, 'X') #=> "abcdX"
+ #
+ def insert(idx, str)
+ pos = idx.to_i
+ pos += self.size + 1 if pos < 0
+
+ raise IndexError, "index #{idx.to_i} out of string" if pos < 0 || pos > self.size
+
+ return self + str if pos == -1
+ return str + self if pos == 0
+ return self[0..pos - 1] + str + self[pos..-1]
+ end
+
+ ##
+ # call-seq:
+ # str.ljust(integer, padstr=' ') -> new_str
+ #
+ # If <i>integer</i> is greater than the length of <i>str</i>, returns a new
+ # <code>String</code> of length <i>integer</i> with <i>str</i> left justified
+ # and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
+ #
+ # "hello".ljust(4) #=> "hello"
+ # "hello".ljust(20) #=> "hello "
+ # "hello".ljust(20, '1234') #=> "hello123412341234123"
+ def ljust(idx, padstr = ' ')
+ if idx <= self.size
+ return self
+ end
+ newstr = self.dup
+ newstr << padstr
+ while newstr.size <= idx
+ newstr << padstr
+ end
+ return newstr.slice(0,idx)
+ end
+
+ # str.upto(other_str, exclusive=false) {|s| block } -> str
+ # str.upto(other_str, exclusive=false) -> an_enumerator
+ #
+ # Iterates through successive values, starting at <i>str</i> and
+ # ending at <i>other_str</i> inclusive, passing each value in turn to
+ # the block. The <code>String#succ</code> method is used to generate
+ # each value. If optional second argument exclusive is omitted or is false,
+ # the last value will be included; otherwise it will be excluded.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+ # "a8".upto("b6") {|s| print s, ' ' }
+ # for s in "a8".."b6"
+ # print s, ' '
+ # end
+ #
+ # <em>produces:</em>
+ #
+ # a8 a9 b0 b1 b2 b3 b4 b5 b6
+ # a8 a9 b0 b1 b2 b3 b4 b5 b6
+ #
+ # If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
+ # both are recognized as decimal numbers. In addition, the width of
+ # string (e.g. leading zeros) is handled appropriately.
+ #
+ # "9".upto("11").to_a #=> ["9", "10", "11"]
+ # "25".upto("5").to_a #=> []
+ # "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
+ #
+ def upto(other_str, excl=false, &block)
+ return to_enum :upto, other_str, excl unless block
+
+ str = self
+ n = self.<=>other_str
+ return self if n > 0 || (self == other_str && excl)
+ while true
+ block.call(str)
+ return self if !excl && str == other_str
+ str = str.succ
+ return self if excl && str == other_str
+ end
+ end
+
+ def chars(&block)
+ if block_given?
+ self.split('').map do |i|
+ block.call(i)
+ end
+ self
+ else
+ self.split('')
+ end
+ end
+ alias each_char chars
+
+ def codepoints(&block)
+ len = self.size
+
+ if block_given?
+ self.split('').map do|x|
+ block.call(x.ord)
+ end
+ self
+ else
+ self.split('').map{|x| x.ord}
+ end
+ end
+ alias each_codepoint codepoints
end