MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / applyQuantifier

Function applyQuantifier

src/pkg/utils/regex_to_glob.ts:683–741  ·  view source on GitHub ↗
(unit: Unit | null)

Source from the content-addressed store, hash-verified

681
682 // ----- Quantifiers ----- 数量词应用
683 function applyQuantifier(unit: Unit | null): string | null {
684 if (unit === null) return null;
685
686 // '*' / '*?' / '*+' —— 任意长度
687 if (peek() === "*") {
688 next();
689 eatQuantMod();
690 return "*";
691 }
692
693 // '+' / '+?' / '++' —— 至少一次:base + '*'
694 if (peek() === "+") {
695 next();
696 eatQuantMod();
697 return (unit.baseGlob || unit.glob) + "*";
698 }
699
700 // '?' / '??' / '?+' —— 可选:统一为 '*'
701 if (peek() === "?") {
702 next();
703 eatQuantMod();
704 return "*";
705 }
706
707 // '{m}', '{m,}', '{m,n}'(懒惰/占有修饰同样处理)
708 if (peek() === "{") {
709 const save = i;
710 next();
711 let num = "";
712 while (/[0-9]/.test(peek())) num += next();
713 if (num === "" || (peek() !== "}" && peek() !== ",")) {
714 i = save;
715 return unit.glob; // 回退为单元字面
716 }
717 const m = parseInt(num, 10);
718 const baseForRepeat = unit.isLiteral ? unit.glob : unit.baseGlob || unit.glob;
719
720 if (eatIf("}")) {
721 eatQuantMod();
722 return baseForRepeat.repeat(m);
723 }
724 if (eatIf(",")) {
725 let num2 = "";
726 while (/[0-9]/.test(peek())) num2 += next();
727 if (!eatIf("}")) {
728 i = save;
729 return unit.glob;
730 }
731 const core = baseForRepeat.repeat(m);
732 eatQuantMod();
733 if (num2 === "") return core + "*"; // {m,}
734 const nmax = parseInt(num2, 10);
735 return core + (Number.isNaN(nmax) || nmax > m ? "*" : ""); // {m,n}
736 }
737 }
738
739 // 无数量词:返回单元 glob
740 return unit.glob;

Callers 1

regexToGlobFunction · 0.85

Calls 4

peekFunction · 0.85
nextFunction · 0.85
eatQuantModFunction · 0.85
eatIfFunction · 0.85

Tested by

no test coverage detected