* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Decoded content
(content)
| 166 | * @return {number[]|string|Uint8Array|Chain} Decoded content |
| 167 | */ |
| 168 | performDecode (content) { |
| 169 | const alphabet = this.getSettingValue('alphabet').getCodePoints() |
| 170 | const separator = this.getSettingValue('separator').getCodePoints() |
| 171 | const includeForeignChars = this.getSettingValue('includeForeignChars') |
| 172 | |
| 173 | const rows = this.getSettingValue('rows').getCodePoints() |
| 174 | const columns = this.getSettingValue('columns').getCodePoints() |
| 175 | |
| 176 | // Ignore all separators inside the given content |
| 177 | const input = ArrayUtil.replaceSlice(content.getCodePoints(), separator) |
| 178 | |
| 179 | const alphabetLength = alphabet.length |
| 180 | const width = columns.length |
| 181 | const inputLength = input.length |
| 182 | const result = new Array(inputLength) |
| 183 | |
| 184 | let i = 0 |
| 185 | let j = 0 |
| 186 | let row, column, index |
| 187 | |
| 188 | while (i < inputLength) { |
| 189 | // Find the next valid row character |
| 190 | row = null |
| 191 | while (row === null && i < inputLength) { |
| 192 | index = rows.indexOf(input[i]) |
| 193 | if (index !== -1) { |
| 194 | // Found row character |
| 195 | row = index |
| 196 | } else if (includeForeignChars) { |
| 197 | // Include foreign character |
| 198 | result[j++] = input[i] |
| 199 | } |
| 200 | i++ |
| 201 | } |
| 202 | |
| 203 | // Find the next valid column character |
| 204 | column = null |
| 205 | while (column === null && i < inputLength) { |
| 206 | index = rows.indexOf(input[i]) |
| 207 | if (index !== -1) { |
| 208 | // Found column character |
| 209 | column = index |
| 210 | } else if (includeForeignChars) { |
| 211 | // Include foreign character |
| 212 | result[j++] = input[i] |
| 213 | } |
| 214 | i++ |
| 215 | } |
| 216 | |
| 217 | if (row !== null && column !== null) { |
| 218 | // Decode square coordinates |
| 219 | index = row * width + column |
| 220 | |
| 221 | // Make sure the given square cell is defined by the alphabet |
| 222 | if (index >= alphabetLength) { |
| 223 | throw new InvalidInputError( |
| 224 | `Polybius square cell at coordinates ${row},${column} are not ` + |
| 225 | 'defined by the alphabet.') |
nothing calls this directly
no test coverage detected