Skip to content

Commit

Permalink
Merge pull request #28 from meaglyn/hex_escapes
Browse files Browse the repository at this point in the history
Add support for \x hex escape in strings
  • Loading branch information
virusman authored Apr 2, 2022
2 parents 7318c07 + 6d0619a commit 29fca89
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions _NscLib/NscContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
//
//-----------------------------------------------------------------------------

#include <ctype.h>
#include <vector>
#include "Precomp.h"
#include "Nsc.h"
Expand Down Expand Up @@ -781,7 +782,7 @@ get_next_token:;

return STRING_CONST;
}
else if (c == '\\')
else if (c == '\\') // String Escapes
{
c = *m_pStreamTop ->pszNextTokenPos;
if (c == 'n')
Expand All @@ -790,17 +791,45 @@ get_next_token:;
m_pStreamTop ->pszNextTokenPos++;
}
else if (c == '"' && !m_DisableNwnEeEscape)
{
*pszOut++ = '"';
m_pStreamTop ->pszNextTokenPos++;
}
{
*pszOut++ = '"';
m_pStreamTop ->pszNextTokenPos++;
}
else if (c == '\\' && !m_DisableNwnEeEscape)
{
*pszOut++ = '\\';
m_pStreamTop ->pszNextTokenPos++;
}
else
;
else if (c == 'x' && !m_DisableNwnEeEscape) {
char vals[3];
int val;
// move past the 'x'
m_pStreamTop ->pszNextTokenPos++;
c = *m_pStreamTop ->pszNextTokenPos;
if (isxdigit(c)) {
vals[0] = c;
m_pStreamTop ->pszNextTokenPos++;
// Check for a second digit only
c = *m_pStreamTop ->pszNextTokenPos;
if (isxdigit(c)) {
vals[1] = c;
vals[2] = 0;
m_pStreamTop ->pszNextTokenPos++;
} else {
vals[1] = 0;
}
val = strtol(vals, NULL, 16);
*pszOut++ = val;
} else {
//error: \x used with no following hex digits
//undo - we've moved passed the x so write it
// inbox compile consumes it. But it also blindly
// consumes following 2 chars even if not hex digits.
*pszOut++ = 'x';
}
}
else
; //warning: unknown escape sequence: '\z'
}
else if (c == 0)
{
Expand Down

0 comments on commit 29fca89

Please sign in to comment.