diff --git a/compiler/src/dmd/frontend.h b/compiler/src/dmd/frontend.h index a5b710cfd503..3c3a7319f8d2 100644 --- a/compiler/src/dmd/frontend.h +++ b/compiler/src/dmd/frontend.h @@ -8920,9 +8920,6 @@ struct Token final }; Identifier* ident; }; - void setString(const char* ptr, size_t length); - void setString(const OutBuffer& buf); - void setString(); const char* toChars() const; static const char* toChars(TOK value); Token() : diff --git a/compiler/src/dmd/lexer.d b/compiler/src/dmd/lexer.d index c51fdf6123e9..540c6640303d 100644 --- a/compiler/src/dmd/lexer.d +++ b/compiler/src/dmd/lexer.d @@ -1635,7 +1635,7 @@ class Lexer case 0: case 0x1A: error("unterminated string constant starting at %s", start.toChars()); - result.setString(); + result.setString(null); // rewind `p` so it points to the EOF character p--; return; @@ -1645,7 +1645,7 @@ class Lexer if (supportInterpolation) result.appendInterpolatedPart(stringbuffer); else - result.setString(stringbuffer); + result.setString(stringbuffer[]); stringPostfix(result); return; @@ -1698,7 +1698,7 @@ class Lexer case 0: case 0x1A: error("unterminated string constant starting at %s", start.toChars()); - t.setString(); + t.setString(null); // decrement `p`, because it needs to point to the next token (the 0 or 0x1A character is the TOK.endOfFile token). p--; return TOK.hexadecimalString; @@ -1708,7 +1708,7 @@ class Lexer error("odd number (%d) of hex characters in hex string", n); stringbuffer.writeByte(cast(char)v); } - t.setString(stringbuffer); + t.setString(stringbuffer[]); stringPostfix(t); return TOK.hexadecimalString; default: @@ -1801,7 +1801,7 @@ class Lexer case 0: case 0x1A: error("unterminated delimited string constant starting at %s", start.toChars()); - result.setString(); + result.setString(null); // decrement `p`, because it needs to point to the next token (the 0 or 0x1A character is the TOK.endOfFile token). p--; return; @@ -1907,7 +1907,7 @@ class Lexer error("delimited string must end in `\"`"); else error(token.loc, "delimited string must end in `%c\"`", delimright); - result.setString(stringbuffersecondary); + result.setString(stringbuffersecondary[]); stringPostfix(result); } @@ -1954,7 +1954,7 @@ class Lexer if (supportInterpolation) result.appendInterpolatedPart(pstart, p - 1 - pstart); else - result.setString(pstart, p - 1 - pstart); + result.setString(pstart[0 .. p - 1 - pstart]); stringPostfix(result); return; @@ -1976,7 +1976,7 @@ class Lexer continue; case TOK.endOfFile: error("unterminated token string constant starting at %s", start.toChars()); - result.setString(); + result.setString(null); return; default: continue; @@ -2121,7 +2121,7 @@ class Lexer if (supportInterpolation) t.appendInterpolatedPart(stringbuffer); else - t.setString(stringbuffer); + t.setString(stringbuffer[]); if (!Ccompile) stringPostfix(t); return; @@ -2131,7 +2131,7 @@ class Lexer p--; Lunterminated: error("unterminated string constant starting at %s", start.toChars()); - t.setString(); + t.setString(null); return; default: if (c & 0x80) diff --git a/compiler/src/dmd/tokens.d b/compiler/src/dmd/tokens.d index 15a571441beb..073e0f30d204 100644 --- a/compiler/src/dmd/tokens.d +++ b/compiler/src/dmd/tokens.d @@ -949,40 +949,23 @@ nothrow: } /**** - * Set to contents of ptr[0..length] + * Set to contents of str * Params: - * ptr = pointer to string - * length = length of string + * str = string */ - void setString(const(char)* ptr, size_t length) + extern (D) void setString(const(char)[] str) { value = TOK.string_; - auto s = cast(char*)mem.xmalloc_noscan(length + 1); - memcpy(s, ptr, length); - s[length] = 0; - ustring = s; - len = cast(uint)length; - postfix = 0; - } - - /**** - * Set to contents of buf - * Params: - * buf = string (not zero terminated) - */ - void setString(const ref OutBuffer buf) - { - setString(cast(const(char)*)buf[].ptr, buf.length); - } - - /**** - * Set to empty string - */ - void setString() - { - value = TOK.string_; - ustring = ""; - len = 0; + len = cast(uint)str.length; + if (len) + { + auto s = cast(char*)mem.xmalloc_noscan(len + 1); + memcpy(s, str.ptr, len); + s[len] = 0; + ustring = s; + } + else + ustring = ""; postfix = 0; }