ID:195067
 
//Title: Compress Text
//Credit To: Popisfizzy

/*
This compresses a text string by removing leading and trailing
spaces. For example, the string " a " will become "a", and
" Hello, how are you? " becomes "Hello, how are you?"
*/



proc/compress_text(string)
var/len = length(string)
var/e, f
for(var/a = 1, a <= len, a ++)
if(text2ascii(string, a) != 32)
if(!e)
e = a
a --
else f = a + 1
if(!e && !f) return ""
if(e) return copytext(string, e, f)
return string

//Testing implimentation

mob/verb/Test(string as text) world << compress_text(string)
Interesting. My solution to this problem involved my substring matching algorithms. I would count the number of spaces at the front and end of the string (each stops iterating as soon as it encounters a symbol that doesn't match), then copy the part of the string that excluded those.
//Title: Compress Text
//Credit To: Popisfizzy

/*
This compresses a text string by removing leading and trailing
spaces. For example, the string " a " will become "a", and
" Hello, how are you? " becomes "Hello, how are you?"
*/


proc/compress_text(string)
var
length = length(string)

start = 1
end = length - 1
for(start, text2ascii(string, start) == 10 ||\
text2ascii(string, start) == 32, start ++)
for(end, text2ascii(string, end) == 10 ||\
text2ascii(string, end) == 32, end --)

return copytext(string, start, end + 1)

//Testing implimentation

mob/verb/Test(string as text) world << compress_text(string)