@@ -280,43 +280,24 @@ public override void WriteHtmlContent(CodeRenderingContext context, HtmlContentI
280280 // Internal for testing
281281 internal void WriteHtmlLiteral ( CodeRenderingContext context , int maxStringLiteralLength , ReadOnlyMemory < char > literal )
282282 {
283- if ( literal . Length <= maxStringLiteralLength )
283+ while ( literal . Length > maxStringLiteralLength )
284284 {
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 ] ;
288287
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 ;
297293
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 ] ) ;
313295
314- var textToRender = literal . Slice ( charactersConsumed , charactersToSubstring ) ;
315-
316- WriteLiteral ( textToRender ) ;
296+ literal = literal [ renderCharCount ..] ;
297+ }
317298
318- charactersConsumed += textToRender . Length ;
319- } while ( charactersConsumed < literal . Length ) ;
299+ WriteLiteral ( literal ) ;
300+ return ;
320301
321302 void WriteLiteral ( ReadOnlyMemory < char > content )
322303 {
0 commit comments