MCPcopy Create free account
hub / github.com/cryptii/cryptii / performEncode

Method performEncode

src/Encoder/Bootstring.js:155–245  ·  view source on GitHub ↗

* Performs encode on given content following section 6.3 of RFC 3492. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content

(content)

Source from the content-addressed store, hash-verified

153 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
154 */
155 performEncode (content) {
156 const {
157 initialBias,
158 initialN,
159 tmin,
160 tmax,
161 caseSensitivity
162 } = this.getSettingValues()
163
164 const delimiter = this.getSettingValue('delimiter').getCodePointAt(0)
165 const base = this.getSettingValue('digitMapping').getLength()
166
167 // Prepare content
168 if (!caseSensitivity) {
169 content = content.toLowerCase()
170 }
171
172 // Initialize the state
173 const input = content.getCodePoints()
174 const inputLength = input.length
175 let n = initialN
176 let bias = initialBias
177 let delta = 0
178
179 // Copy basic code points in the input to the output in order
180 const output = []
181 for (let i = 0; i < input.length; i++) {
182 if (this._isBasic(input[i])) {
183 output.push(input[i])
184 } else if (input[i] < n) {
185 throw new InvalidInputError(
186 `Unexpected code point at index ${i}, consider changing initial n ` +
187 'to include this code point')
188 }
189 }
190
191 let h = output.length
192 const b = output.length
193 if (b > 0) {
194 output.push(delimiter)
195 }
196
197 let m, q, k, t, c
198 while (h < inputLength) {
199 // Find the next larger non-basic code point >= n
200 m = Number.MAX_SAFE_INTEGER
201 for (c of input) {
202 if (c >= n && c < m && !this._isBasic(c)) {
203 m = c
204 }
205 }
206
207 // Increase delta enough to advance the decoder's <n,i> state to <m,0>,
208 // but guard against overflow
209 if (m - n > MathUtil.div(Number.MAX_SAFE_INTEGER - delta, h + 1)) {
210 throw new InvalidInputError(integerOverflowMessage)
211 }
212 delta += (m - n) * (h + 1)

Callers

nothing calls this directly

Calls 12

_isBasicMethod · 0.95
_calcTMethod · 0.95
_adaptBiasMethod · 0.95
getSettingValuesMethod · 0.80
getCodePointAtMethod · 0.80
getSettingValueMethod · 0.80
toLowerCaseMethod · 0.80
getCodePointsMethod · 0.80
divMethod · 0.80
modMethod · 0.80
getLengthMethod · 0.45

Tested by

no test coverage detected