@@ -604,14 +604,18 @@ function isHTTPWhiteSpace (char) {
604604 * @param {boolean } [trailing=true]
605605 */
606606function removeHTTPWhitespace ( str , leading = true , trailing = true ) {
607- let i = 0 ; let j = str . length
607+ let lead = 0
608+ let trail = str . length - 1
609+
608610 if ( leading ) {
609- while ( j > i && isHTTPWhiteSpace ( str . charCodeAt ( i ) ) ) -- i
611+ while ( lead < str . length && isHTTPWhiteSpace ( str . charCodeAt ( lead ) ) ) lead ++
610612 }
613+
611614 if ( trailing ) {
612- while ( j > i && isHTTPWhiteSpace ( str . charCodeAt ( j - 1 ) ) ) -- j
615+ while ( trail > 0 && isHTTPWhiteSpace ( str . charCodeAt ( trail ) ) ) trail --
613616 }
614- return i === 0 && j === str . length ? str : str . substring ( i , j )
617+
618+ return lead === 0 && trail === str . length - 1 ? str : str . slice ( lead , trail + 1 )
615619}
616620
617621/**
@@ -630,14 +634,18 @@ function isASCIIWhitespace (char) {
630634 * @param {boolean } [trailing=true]
631635 */
632636function removeASCIIWhitespace ( str , leading = true , trailing = true ) {
633- let i = 0 ; let j = str . length
637+ let lead = 0
638+ let trail = str . length - 1
639+
634640 if ( leading ) {
635- while ( j > i && isASCIIWhitespace ( str . charCodeAt ( i ) ) ) -- i
641+ while ( lead < str . length && isASCIIWhitespace ( str . charCodeAt ( lead ) ) ) lead ++
636642 }
643+
637644 if ( trailing ) {
638- while ( j > i && isASCIIWhitespace ( str . charCodeAt ( j - 1 ) ) ) -- j
645+ while ( trail > 0 && isASCIIWhitespace ( str . charCodeAt ( trail ) ) ) trail --
639646 }
640- return i === 0 && j === str . length ? str : str . substring ( i , j )
647+
648+ return lead === 0 && trail === str . length - 1 ? str : str . slice ( lead , trail + 1 )
641649}
642650
643651module . exports = {
0 commit comments