* Processes the provided str. * @param {string} str string * @returns {void}
(str)
| 46 | * @returns {void} |
| 47 | */ |
| 48 | add(str) { |
| 49 | const len = str.length; |
| 50 | const value = this._value; |
| 51 | if (value === undefined) { |
| 52 | /** |
| 53 | * We are choosing to use Buffer.allocUnsafe() because it is often faster than Buffer.alloc() because |
| 54 | * it allocates a new buffer of the specified size without initializing the memory. |
| 55 | */ |
| 56 | const newValue = (this._value = Buffer.allocUnsafe(len)); |
| 57 | for (let i = 0; i < len; i++) { |
| 58 | newValue[i] = str.charCodeAt(i); |
| 59 | } |
| 60 | return; |
| 61 | } |
| 62 | const valueLen = value.length; |
| 63 | if (valueLen < len) { |
| 64 | const newValue = (this._value = Buffer.allocUnsafe(len)); |
| 65 | /** @type {number} */ |
| 66 | let i; |
| 67 | for (i = 0; i < valueLen; i++) { |
| 68 | newValue[i] = value[i] ^ str.charCodeAt(i); |
| 69 | } |
| 70 | for (; i < len; i++) { |
| 71 | newValue[i] = str.charCodeAt(i); |
| 72 | } |
| 73 | } else { |
| 74 | for (let i = 0; i < len; i++) { |
| 75 | // eslint-disable-next-line operator-assignment |
| 76 | value[i] = value[i] ^ str.charCodeAt(i); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns a string that represents the current state of the StringXor object. We chose to use "latin1" encoding |
no outgoing calls
no test coverage detected