@@ -267,107 +267,92 @@ internal static string GetText(XmlTextSyntax textElement, bool normalizeWhitespa
267267 return null ;
268268 }
269269
270- string result = string . Empty ;
270+ bool lastWhitespace = false ;
271+
272+ string single = string . Empty ;
271273
272274 StringBuilder stringBuilder = null ;
273275
274276 foreach ( var item in textElement . TextTokens )
275277 {
276- if ( result . Length == 0 )
278+ if ( single . Length == 0 )
277279 {
278- result = item . ToString ( ) ;
280+ single = item . ToString ( ) ;
279281 }
280282 else
281283 {
282284 if ( stringBuilder == null )
283285 {
284286 stringBuilder = StringBuilderPool . Allocate ( ) ;
285- stringBuilder . Append ( result ) ;
287+ stringBuilder . AppendNormalize ( single , normalizeWhitespace , ref lastWhitespace ) ;
286288 }
287289
288- stringBuilder . Append ( item . ToString ( ) ) ;
290+ stringBuilder . AppendNormalize ( item . ToString ( ) , normalizeWhitespace , ref lastWhitespace ) ;
289291 }
290292 }
291293
292- if ( stringBuilder != null )
293- {
294- result = StringBuilderPool . ReturnAndFree ( stringBuilder ) ;
295- }
296-
297- if ( normalizeWhitespace )
298- {
299- result = result . NormalizeWhiteSpace ( ) ;
300- }
301-
302- return result ;
303- }
304-
305- internal static string NormalizeWhiteSpace ( this string text )
306- {
307- if ( text == null )
294+ if ( stringBuilder == null )
308295 {
309- return null ;
310- }
311-
312- int length = text . Length ;
313-
314- bool lastSpace = false ;
315-
316- bool diff = false ;
317-
318- foreach ( char ch in text )
319- {
320- if ( char . IsWhiteSpace ( ch ) )
296+ if ( normalizeWhitespace )
321297 {
322- if ( lastSpace )
323- {
324- length -- ;
325- }
326- else
298+ stringBuilder = StringBuilderPool . Allocate ( ) ;
299+
300+ if ( ! stringBuilder . AppendNormalize ( single , normalizeWhitespace , ref lastWhitespace ) )
327301 {
328- if ( ch != ' ' )
329- {
330- diff = true ;
331- }
302+ StringBuilderPool . Free ( stringBuilder ) ;
332303
333- lastSpace = true ;
304+ return single ;
334305 }
335306 }
336307 else
337308 {
338- lastSpace = false ;
309+ return single ;
339310 }
340311 }
341312
342- if ( diff || ( length != text . Length ) )
343- {
344- char [ ] buffer = new char [ length ] ;
345-
346- lastSpace = false ;
313+ return StringBuilderPool . ReturnAndFree ( stringBuilder ) ;
314+ }
347315
348- length = 0 ;
316+ internal static bool AppendNormalize ( this StringBuilder builder , string text , bool normalizeWhitespace , ref bool lastWhitespace )
317+ {
318+ bool diff = false ;
349319
320+ if ( normalizeWhitespace )
321+ {
350322 foreach ( char ch in text )
351323 {
352324 if ( char . IsWhiteSpace ( ch ) )
353325 {
354- if ( ! lastSpace )
326+ if ( lastWhitespace )
355327 {
356- buffer [ length ++ ] = ' ' ;
357- lastSpace = true ;
328+ diff = true ;
329+ }
330+ else
331+ {
332+ if ( ch != ' ' )
333+ {
334+ diff = true ;
335+ }
336+
337+ builder . Append ( ' ' ) ;
358338 }
339+
340+ lastWhitespace = true ;
359341 }
360342 else
361343 {
362- buffer [ length ++ ] = ch ;
363- lastSpace = false ;
344+ builder . Append ( ch ) ;
345+
346+ lastWhitespace = false ;
364347 }
365348 }
366-
367- return new string ( buffer , 0 , length ) ;
349+ }
350+ else
351+ {
352+ builder . Append ( text ) ;
368353 }
369354
370- return text ;
355+ return diff ;
371356 }
372357
373358 internal static T GetFirstAttributeOrDefault < T > ( XmlNodeSyntax nodeSyntax )
0 commit comments