@@ -280,43 +280,24 @@ public override void WriteHtmlContent(CodeRenderingContext context, HtmlContentI
280
280
// Internal for testing
281
281
internal void WriteHtmlLiteral ( CodeRenderingContext context , int maxStringLiteralLength , ReadOnlyMemory < char > literal )
282
282
{
283
- if ( literal . Length <= maxStringLiteralLength )
283
+ while ( literal . Length > maxStringLiteralLength )
284
284
{
285
- WriteLiteral ( literal ) ;
286
- return ;
287
- }
285
+ // String is too large, render the string in pieces to avoid Roslyn OOM exceptions at compile time: https://github.com/aspnet/External/issues/54
286
+ var lastCharBeforeSplit = literal . Span [ maxStringLiteralLength - 1 ] ;
288
287
289
- // String is too large, render the string in pieces to avoid Roslyn OOM exceptions at compile time: https://github.com/aspnet/External/issues/54
290
- var charactersConsumed = 0 ;
291
- do
292
- {
293
- var charactersRemaining = literal . Length - charactersConsumed ;
294
- var charactersToSubstring = Math . Min ( maxStringLiteralLength , charactersRemaining ) ;
295
- var lastCharBeforeSplitIndex = charactersConsumed + charactersToSubstring - 1 ;
296
- var lastCharBeforeSplit = literal . Span [ lastCharBeforeSplitIndex ] ;
288
+ // If character at splitting point is a high surrogate, take one less character this iteration
289
+ // as we're attempting to split a surrogate pair. This can happen when something like an
290
+ // emoji sits on the barrier between splits; if we were to split the emoji we'd end up with
291
+ // invalid bytes in our output.
292
+ var renderCharCount = char . IsHighSurrogate ( lastCharBeforeSplit ) ? maxStringLiteralLength - 1 : maxStringLiteralLength ;
297
293
298
- if ( char . IsHighSurrogate ( lastCharBeforeSplit ) )
299
- {
300
- if ( charactersRemaining > 1 )
301
- {
302
- // Take one less character this iteration. We're attempting to split inbetween a surrogate pair.
303
- // This can happen when something like an emoji sits on the barrier between splits; if we were to
304
- // split the emoji we'd end up with invalid bytes in our output.
305
- charactersToSubstring -- ;
306
- }
307
- else
308
- {
309
- // The user has an invalid file with a partial surrogate a the splitting point.
310
- // We'll let the invalid character flow but we'll explode later on.
311
- }
312
- }
294
+ WriteLiteral ( literal [ ..renderCharCount ] ) ;
313
295
314
- var textToRender = literal . Slice ( charactersConsumed , charactersToSubstring ) ;
315
-
316
- WriteLiteral ( textToRender ) ;
296
+ literal = literal [ renderCharCount ..] ;
297
+ }
317
298
318
- charactersConsumed += textToRender . Length ;
319
- } while ( charactersConsumed < literal . Length ) ;
299
+ WriteLiteral ( literal ) ;
300
+ return ;
320
301
321
302
void WriteLiteral ( ReadOnlyMemory < char > content )
322
303
{
0 commit comments