forked from mruby/mruby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mruby#967 from iij/pr-string-strip
add "strip" family methods of String to mruby-string-ext
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters