Skip to content

Commit 152dfbc

Browse files
authored
implements trimtext() (#1114)
1 parent 20039ab commit 152dfbc

File tree

2 files changed

+4
-25
lines changed

2 files changed

+4
-25
lines changed

code/__HELPERS/text.dm

+1-19
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,6 @@
284284
return copytext(text, 1, i + 1)
285285
return ""
286286

287-
//Returns a string with reserved characters and spaces after the first and last letters removed
288-
//Like trim(), but very slightly faster. worth it for niche usecases
289-
/proc/trim_reduced(text)
290-
var/starting_coord = 1
291-
var/text_len = length(text)
292-
for (var/i in 1 to text_len)
293-
if (text2ascii(text, i) > 32)
294-
starting_coord = i
295-
break
296-
297-
for (var/i = text_len, i >= starting_coord, i--)
298-
if (text2ascii(text, i) > 32)
299-
return copytext(text, starting_coord, i + 1)
300-
301-
if(starting_coord > 1)
302-
return copytext(text, starting_coord)
303-
return ""
304-
305287
/**
306288
* Truncate a string to the given length
307289
*
@@ -322,7 +304,7 @@
322304
/proc/trim(text, max_length)
323305
if(max_length)
324306
text = copytext_char(text, 1, max_length)
325-
return trim_reduced(text)
307+
return trimtext(text)
326308

327309
//Returns a string with the first element of the string capitalized.
328310
/proc/capitalize(t)

code/modules/mapping/reader.dm

+3-6
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@
115115
var/turfsSkipped = 0
116116
#endif
117117

118-
//text trimming (both directions) helper macro
119-
#define TRIM_TEXT(text) (trim_reduced(text))
120-
121118
/// Shortcut function to parse a map and apply it to the world.
122119
///
123120
/// - `dmm_file`: A .dmm file to load (Required).
@@ -722,7 +719,7 @@ GLOBAL_LIST_EMPTY(map_model_default)
722719
if(member_string[length(member_string)] == "}")
723720
variables_start = findtext(member_string, "{")
724721

725-
var/path_text = TRIM_TEXT(copytext(member_string, 1, variables_start))
722+
var/path_text = trimtext(copytext(member_string, 1, variables_start))
726723
var/atom_def = text2path(path_text) //path definition, e.g /obj/foo/bar
727724

728725
if(!ispath(atom_def, /atom)) // Skip the item if the path does not exist. Fix your crap, mappers!
@@ -898,7 +895,7 @@ GLOBAL_LIST_EMPTY(map_model_default)
898895

899896
// check if this is a simple variable (as in list(var1, var2)) or an associative one (as in list(var1="foo",var2=7))
900897
var/equal_position = findtext(text,"=",old_position, position)
901-
var/trim_left = TRIM_TEXT(copytext(text,old_position,(equal_position ? equal_position : position)))
898+
var/trim_left = trimtext(copytext(text,old_position,(equal_position ? equal_position : position)))
902899
var/left_constant = parse_constant(trim_left)
903900
if(position)
904901
old_position = position + length(text[position])
@@ -908,7 +905,7 @@ GLOBAL_LIST_EMPTY(map_model_default)
908905
if(equal_position && !isnum(left_constant))
909906
// Associative var, so do the association.
910907
// Note that numbers cannot be keys - the RHS is dropped if so.
911-
var/trim_right = TRIM_TEXT(copytext(text, equal_position + length(text[equal_position]), position))
908+
var/trim_right = trimtext(copytext(text, equal_position + length(text[equal_position]), position))
912909
var/right_constant = parse_constant(trim_right)
913910
.[left_constant] = right_constant
914911
else // simple var

0 commit comments

Comments
 (0)