Skip to content

Commit

Permalink
Changed commands to avoid trimming literal "t" or " " around args.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexikos committed Jan 6, 2012
1 parent 0bd7465 commit c52d085
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/script.cpp
Expand Up @@ -4779,9 +4779,9 @@ ResultType Script::ParseAndAddLine(LPTSTR aLineText, ActionTypeType aActionType,
// because the map is still accurate due to the nature of rtrim). UPDATE: Note that this
// version of rtrim() specifically avoids trimming newline characters, since the user may
// have included literal newlines at the end of the string by using an escape sequence:
rtrim(arg[nArgs]);
rtrim_literal(arg[nArgs], arg_map[nArgs]);
// Omit the leading whitespace from the next arg:
for (++mark; IS_SPACE_OR_TAB(action_args[mark]); ++mark);
for (++mark; IS_SPACE_OR_TAB(action_args[mark]) && !literal_map[mark]; ++mark);
// Now <mark> marks the end of the string, the start of the next arg,
// or a delimiter-char (if the next arg is blank).
}
Expand Down
30 changes: 30 additions & 0 deletions source/util.h
Expand Up @@ -296,6 +296,36 @@ inline size_t rtrim(LPTSTR aStr, size_t aLength = -1)
}
}

inline void rtrim_literal(LPTSTR aStr, TCHAR aLiteralMap[])
// Caller must ensure that aStr is not NULL.
// NOTE: THIS VERSION trims only tabs and spaces which aren't marked as literal (so not "`t" or "` ").
// It specifically avoids trimming newlines because some callers want to retain those.
{
if (!*aStr) return; // The below relies upon this check having been done.
// It's done this way in case aStr just happens to be address 0x00 (probably not possible
// on Intel & Intel-clone hardware) because otherwise --cp would decrement, causing an
// underflow since pointers are probably considered unsigned values, which would
// probably cause an infinite loop. Extremely unlikely, but might as well try
// to be thorough:
for (size_t last = _tcslen(aStr) - 1; ; --last)
{
if (!IS_SPACE_OR_TAB(aStr[last]) || aLiteralMap[last]) // It's not a space or tab, or it's a literal one.
{
aStr[last + 1] = '\0';
return;
}
// Otherwise, it is a space or tab...
if (last == 0) // ... and we're now at the first character of the string...
{
if (IS_SPACE_OR_TAB(aStr[last])) // ... and that first character is also a space or tab...
*aStr = '\0'; // ... so the entire string is made empty.
return; // ... and we return in any case.
}
// else it's a space or tab, and there are still more characters to check. Let the loop
// do its decrements.
}
}

inline size_t rtrim_with_nbsp(LPTSTR aStr, size_t aLength = -1)
// Returns the new length of the string.
// Caller must ensure that aStr is not NULL.
Expand Down

0 comments on commit c52d085

Please sign in to comment.