mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
add "strip" family to String.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
class String
|
||||
def lstrip
|
||||
a = 0
|
||||
z = self.size - 1
|
||||
a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
|
||||
(z >= 0) ? self[a..z] : ""
|
||||
end
|
||||
|
||||
def rstrip
|
||||
a = 0
|
||||
z = self.size - 1
|
||||
z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
|
||||
(z >= 0) ? self[a..z] : ""
|
||||
end
|
||||
|
||||
def strip
|
||||
a = 0
|
||||
z = self.size - 1
|
||||
a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
|
||||
z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
|
||||
(z >= 0) ? self[a..z] : ""
|
||||
end
|
||||
|
||||
def lstrip!
|
||||
s = self.lstrip
|
||||
(s == self) ? nil : self.replace(s)
|
||||
end
|
||||
|
||||
def rstrip!
|
||||
s = self.rstrip
|
||||
(s == self) ? nil : self.replace(s)
|
||||
end
|
||||
|
||||
def strip!
|
||||
s = self.strip
|
||||
(s == self) ? nil : self.replace(s)
|
||||
end
|
||||
end
|
||||
@@ -16,3 +16,57 @@ end
|
||||
assert('String#dump') do
|
||||
"foo".dump == "\"foo\""
|
||||
end
|
||||
|
||||
assert('String#strip') do
|
||||
s = " abc "
|
||||
s.strip
|
||||
"".strip == "" and " \t\r\n\f\v".strip == "" and
|
||||
"\0a\0".strip == "\0a" and
|
||||
"abc".strip == "abc" and
|
||||
" abc".strip == "abc" and
|
||||
"abc ".strip == "abc" and
|
||||
" abc ".strip == "abc" and
|
||||
s == " abc "
|
||||
end
|
||||
|
||||
assert('String#lstrip') do
|
||||
s = " abc "
|
||||
s.lstrip
|
||||
"".lstrip == "" and " \t\r\n\f\v".lstrip == "" and
|
||||
"\0a\0".lstrip == "\0a\0" and
|
||||
"abc".lstrip == "abc" and
|
||||
" abc".lstrip == "abc" and
|
||||
"abc ".lstrip == "abc " and
|
||||
" abc ".lstrip == "abc " and
|
||||
s == " abc "
|
||||
end
|
||||
|
||||
assert('String#rstrip') do
|
||||
s = " abc "
|
||||
s.rstrip
|
||||
"".rstrip == "" and " \t\r\n\f\v".rstrip == "" and
|
||||
"\0a\0".rstrip == "\0a" and
|
||||
"abc".rstrip == "abc" and
|
||||
" abc".rstrip == " abc" and
|
||||
"abc ".rstrip == "abc" and
|
||||
" abc ".rstrip == " abc" and
|
||||
s == " abc "
|
||||
end
|
||||
|
||||
assert('String#strip!') do
|
||||
s = " abc "
|
||||
t = "abc"
|
||||
s.strip! == "abc" and s == "abc" and t.strip! == nil
|
||||
end
|
||||
|
||||
assert('String#lstrip!') do
|
||||
s = " abc "
|
||||
t = "abc "
|
||||
s.lstrip! == "abc " and s == "abc " and t.lstrip! == nil
|
||||
end
|
||||
|
||||
assert('String#rstrip!') do
|
||||
s = " abc "
|
||||
t = " abc"
|
||||
s.rstrip! == " abc" and s == " abc" and t.rstrip! == nil
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user