Merge pull request #17165 from calixteman/improve_removenullchars
Slightly improve the performance of removeNullCharacters
This commit is contained in:
		
						commit
						f27f2bb403
					
				| @ -206,19 +206,20 @@ function parseQueryString(query) { | |||||||
|   return params; |   return params; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const InvisibleCharactersRegExp = /[\x01-\x1F]/g; | const InvisibleCharactersRegExp = /[\x00-\x1F]/g; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @param {string} str |  * @param {string} str | ||||||
|  * @param {boolean} [replaceInvisible] |  * @param {boolean} [replaceInvisible] | ||||||
|  */ |  */ | ||||||
| function removeNullCharacters(str, replaceInvisible = false) { | function removeNullCharacters(str, replaceInvisible = false) { | ||||||
|   if (typeof str !== "string") { |   if (!InvisibleCharactersRegExp.test(str)) { | ||||||
|     console.error(`The argument must be a string.`); |  | ||||||
|     return str; |     return str; | ||||||
|   } |   } | ||||||
|   if (replaceInvisible) { |   if (replaceInvisible) { | ||||||
|     str = str.replaceAll(InvisibleCharactersRegExp, " "); |     return str.replaceAll(InvisibleCharactersRegExp, m => { | ||||||
|  |       return m === "\x00" ? "" : " "; | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
|   return str.replaceAll("\x00", ""); |   return str.replaceAll("\x00", ""); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user