Skip to content

Instantly share code, notes, and snippets.

@manveru
Created June 20, 2010 05:17
Show Gist options
  • Save manveru/445583 to your computer and use it in GitHub Desktop.
Save manveru/445583 to your computer and use it in GitHub Desktop.
String << = (other|
self concat: other
)
String from:to: = (start end|
temp = ""
(start to: end) do: [i|
char = self at: i
(char == nil) unless: [|
temp concat: char
]
]
temp
)
String from: = (start|
end = (size - 1)
from: start to: end
)
String to: = (end|
start = 0
from: start to: end
)
String reverse = (|
(size > 1) if: [|
from: (size - 1) to: 0
] else: [|
self
]
)
String each: = (aBlock|
(0 to: (self size - 1)) do: [i|
aBlock call: (self at: i)
]
)
String each-byte: = (aBlock|
(0 to: (self size - 1)) do: [i|
aBlock call: (self byte-at: i)
]
)
String map: = (aBlock|
new = ""
self each: [c|
new concat: (aBlock call: c)
]
new
)
String uppercase = (|
temp = clone
(0 to: (size - 1)) do: [i|
byte = self byte-at: i
((byte >= 97) && (byte <= 122)) if: [|
temp put-byte: (byte - 32) at: i
] else: [|
temp put-byte: byte at: i
]
]
temp
)
String capitalize = (|
cap = self at: 0
rest = self from: 1 to: size
(cap uppercase) + rest
)
String =~ = (other|
# matching stuff
regex = Regexp from-string: other
regex match: self
)
String scan: = (string|
regex = Regexp from-string: string
match = regex match: self
match data
)
String gsub:with: = (regex_string string|
regex = Regexp from-string: regex_string
regex replace-all: string on: self
)
String sub:with: = (regex_string string|
regex = Regexp from-string: regex_string
regex replace: string on: self
)
String chop = (|
self from: 0 to: (self size - 2)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment