forked from chalk/chalk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance greatly (chalk#337)
- Loading branch information
Showing
6 changed files
with
159 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const stringReplaceAll = (string, substring, replacer) => { | ||
let index = string.indexOf(substring); | ||
if (index === -1) { | ||
return string; | ||
} | ||
|
||
const subLen = substring.length; | ||
let end = 0; | ||
let res = ''; | ||
do { | ||
res += string.substr(end, index - end) + replacer; | ||
end = index + subLen; | ||
index = string.indexOf(substring, end); | ||
} while (index !== -1); | ||
|
||
res += string.substr(end); | ||
return res; | ||
}; | ||
|
||
const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => { | ||
let end = 0; | ||
let res = ''; | ||
do { | ||
const gotCR = string[index - 1] === '\r'; | ||
res += string.substr(end, (gotCR ? index - 1 : index) - end) + prefix + (gotCR ? '\r\n' : '\n') + postfix; | ||
end = index + 1; | ||
index = string.indexOf('\n', end); | ||
} while (index !== -1); | ||
|
||
res += string.substr(end); | ||
return res; | ||
}; | ||
|
||
module.exports = { | ||
stringReplaceAll, | ||
stringEncaseCRLFWithFirstIndex | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters