Skip to content

Commit

Permalink
implements trimtext() (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapu1178 authored Nov 16, 2024
1 parent 20039ab commit 152dfbc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
20 changes: 1 addition & 19 deletions code/__HELPERS/text.dm
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,6 @@
return copytext(text, 1, i + 1)
return ""

//Returns a string with reserved characters and spaces after the first and last letters removed
//Like trim(), but very slightly faster. worth it for niche usecases
/proc/trim_reduced(text)
var/starting_coord = 1
var/text_len = length(text)
for (var/i in 1 to text_len)
if (text2ascii(text, i) > 32)
starting_coord = i
break

for (var/i = text_len, i >= starting_coord, i--)
if (text2ascii(text, i) > 32)
return copytext(text, starting_coord, i + 1)

if(starting_coord > 1)
return copytext(text, starting_coord)
return ""

/**
* Truncate a string to the given length
*
Expand All @@ -322,7 +304,7 @@
/proc/trim(text, max_length)
if(max_length)
text = copytext_char(text, 1, max_length)
return trim_reduced(text)
return trimtext(text)

//Returns a string with the first element of the string capitalized.
/proc/capitalize(t)
Expand Down
9 changes: 3 additions & 6 deletions code/modules/mapping/reader.dm
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@
var/turfsSkipped = 0
#endif

//text trimming (both directions) helper macro
#define TRIM_TEXT(text) (trim_reduced(text))

/// Shortcut function to parse a map and apply it to the world.
///
/// - `dmm_file`: A .dmm file to load (Required).
Expand Down Expand Up @@ -722,7 +719,7 @@ GLOBAL_LIST_EMPTY(map_model_default)
if(member_string[length(member_string)] == "}")
variables_start = findtext(member_string, "{")

var/path_text = TRIM_TEXT(copytext(member_string, 1, variables_start))
var/path_text = trimtext(copytext(member_string, 1, variables_start))
var/atom_def = text2path(path_text) //path definition, e.g /obj/foo/bar

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

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

0 comments on commit 152dfbc

Please sign in to comment.