Skip to content

Commit 81a05a6

Browse files
authored
Fix quad quotes (#361)
1 parent 790c074 commit 81a05a6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/GraphQLParser.Tests/Visitors/SDLPrinterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ public async Task SDLPrinter_Should_Print_Document(
623623
[InlineData(30, "Test \\ escaping", "Test \\\\ escaping", false)]
624624
[InlineData(31, "t\"est\n line2", "\nt\"est\nline2\n", true)] // BlockString with double quote inside
625625
[InlineData(32, "t\\\"\"\"est\n line2", "\nt\\\"\"\"est\nline2\n", true)] // BlockString with triple double quote inside
626+
[InlineData(32, "t\\\"\"\"\"est\n line2", "\nt\\\"\"\"\"est\nline2\n", true)] // BlockString with quad double quote inside
626627
public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input, string expected, bool isBlockString)
627628
{
628629
number.ShouldBeGreaterThan(0);

src/GraphQLParser/LexerContext.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,30 @@ private Token ReadBlockString()
217217
// if last character was \ then go ahead and write out the """, skipping the \
218218
if (escape)
219219
{
220+
// write """ to buffer
221+
if ((index + 2) < buffer.Length)
222+
{
223+
buffer[index++] = '\"';
224+
buffer[index++] = '\"';
225+
buffer[index++] = '\"';
226+
}
227+
else // fallback to StringBuilder in case of buffer overflow
228+
{
229+
sb ??= new StringBuilder(buffer.Length * 2);
230+
231+
for (int i = 0; i < index; ++i)
232+
sb.Append(buffer[i]);
233+
234+
sb.Append("\"\"\"");
235+
index = 0;
236+
}
237+
// skip \""" from source
238+
_currentIndex += 2;
220239
escape = false;
240+
lastWasCr = false;
241+
// advance to next character
242+
code = NextCode();
243+
continue;
221244
}
222245
else
223246
{

src/GraphQLParser/Visitors/SDLPrinter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ async ValueTask WriteMultilineBlockString()
134134
case '"':
135135
if (i < length - 2 && description.Value.Span[i + 1] == '"' && description.Value.Span[i + 2] == '"')
136136
{
137-
await context.WriteAsync("\\\"").ConfigureAwait(false);
137+
await context.WriteAsync("\\\"\"\"").ConfigureAwait(false);
138+
i += 2;
138139
}
139140
else
140141
{

0 commit comments

Comments
 (0)