(
input: string,
targetRadix?: NumericStringRadix,
)
| 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) { |
no test coverage detected