MCPcopy
hub / github.com/VSCodeVim/Vim / parse

Method parse

src/common/number/numericString.ts:64–143  ·  view source on GitHub ↗
(
    input: string,
    targetRadix?: NumericStringRadix,
  )

Source from the content-addressed store, hash-verified

62
63 // Return parse result and offset of suffix
64 public static parse(
65 input: string,
66 targetRadix?: NumericStringRadix,
67 ): { num: NumericString; suffixOffset: number } | undefined {
68 const filteredMatchings =
69 targetRadix !== undefined
70 ? NumericString.matchings.filter((matching) => matching.radix === targetRadix)
71 : NumericString.matchings;
72
73 // Find core numeric part of input
74 let coreBegin = -1;
75 let coreLength = -1;
76 let coreRadix = -1;
77 let coreSign = false;
78 for (const { regex, radix } of filteredMatchings) {
79 const match = regex.exec(input);
80 if (match != null) {
81 // Get the leftmost and largest match
82 if (
83 coreRadix < 0 ||
84 match.index < coreBegin ||
85 (match.index === coreBegin && match[0].length > coreLength)
86 ) {
87 coreBegin = match.index;
88 coreLength = match[0].length;
89 coreRadix = radix;
90 coreSign = match[1] === '-';
91 }
92 }
93 }
94
95 if (coreRadix < 0) {
96 return undefined;
97 }
98
99 const coreEnd = coreBegin + coreLength;
100
101 const prefix = input.slice(0, coreBegin);
102 const core = input.slice(coreBegin, coreEnd);
103 const suffix = input.slice(coreEnd, input.length);
104
105 let value = parseInt(core, coreRadix);
106
107 // 0x00ff: numLength = 4
108 // 077: numLength = 2
109 // -0999: numLength = 3
110 // The numLength is only useful for parsing non-decimal. Decimal with
111 // leading zero will be trimmed in `toString()`. If value is negative,
112 // remove the width of negative sign.
113 const numLength = coreLength - NumericString.numPrefix[coreRadix].length - (coreSign ? 1 : 0);
114
115 // According to original vim's behavior, for hex and octal, the leading
116 // '-' *should* be captured and preserved but *should not* be regarded as
117 // part of number, which means with <C-a>, `-0xf` turns into `-0x10`. So
118 // for hex and octal, we make the value absolute and set the negative
119 // sign flag.
120 let negative = false;
121 if (coreRadix !== 10 && coreSign) {

Callers 15

execFunction · 0.45
loadFromDiskMethod · 0.45
constructorMethod · 0.45
searchStringMethod · 0.45
handleRemappingMethod · 0.45
_createMarkDecorationMethod · 0.45
loadMethod · 0.45
existsAsyncFunction · 0.45
unlinkFunction · 0.45
readFileAsyncFunction · 0.45
mkdirAsyncFunction · 0.45
writeFileAsyncFunction · 0.45

Calls 1

execMethod · 0.65

Tested by

no test coverage detected