Skip to content

Commit

Permalink
count_match_string, replace_string 코드 최적화
Browse files Browse the repository at this point in the history
  • Loading branch information
usbsync committed Dec 16, 2024
1 parent 70b64e4 commit c3b8c38
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/playground/blocks/block_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2060,15 +2060,7 @@ module.exports = {
func(sprite, script) {
const originStr = script.getStringValue('STRING', script);
const targetStr = script.getStringValue('TARGET', script);

let count = 0;
const substrLength = targetStr.length;
for (let i = 0; i <= originStr.length - substrLength; i++) {
if (originStr.substring(i, i + substrLength) === targetStr) {
count++;
}
}
return count;
return originStr.split(targetStr).length - 1;
},
syntax: {
js: [],
Expand Down Expand Up @@ -2250,16 +2242,12 @@ module.exports = {
class: 'calc_string',
isNotFor: [],
func(sprite, script) {
const oldWord = script
const oldWord2 = script
.getStringValue('OLD_WORD', script)
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

return script
.getStringValue('STRING', script)
.replace(
new RegExp(oldWord, 'gm'),
script.getStringValue('NEW_WORD', script)
);
const newWord = script.getStringValue('NEW_WORD', script);
const originalString = script.getStringValue('STRING', script);
return originalString.split(oldWord2).join(newWord);
},
syntax: {
js: [],
Expand Down

0 comments on commit c3b8c38

Please sign in to comment.