SYS-REQ-115
(config Config, in, out []byte)
| 287 | |
| 288 | // SYS-REQ-115 |
| 289 | func unescapeConfig(config Config, in, out []byte) ([]byte, error) { |
| 290 | firstBackslash := bytes.IndexByte(in, '\\') |
| 291 | if firstBackslash == -1 { |
| 292 | return in, nil |
| 293 | } |
| 294 | |
| 295 | // Get a buffer of sufficient size (allocate if needed) |
| 296 | if cap(out) < len(in) { |
| 297 | out = make([]byte, len(in)) |
| 298 | } else { |
| 299 | out = out[0:len(in)] |
| 300 | } |
| 301 | |
| 302 | // Copy the first sequence of unescaped bytes to the output and obtain a buffer pointer (subslice) |
| 303 | copy(out, in[:firstBackslash]) |
| 304 | in = in[firstBackslash:] |
| 305 | buf := out[firstBackslash:] |
| 306 | |
| 307 | // The loop always exits via break: either on error (MalformedStringEscapeError) |
| 308 | // or after copying the final non-escaped tail. The former `for len(in) > 0` |
| 309 | // guard was structurally always true on re-entry since the else branch always |
| 310 | // leaves at least the backslash character in `in`. |
| 311 | for { |
| 312 | // Unescape the next escaped character |
| 313 | inLen, bufLen := unescapeToUTF8Config(config, in, buf) |
| 314 | if inLen == -1 { |
| 315 | return nil, MalformedStringEscapeError |
| 316 | } |
| 317 | |
| 318 | in = in[inLen:] |
| 319 | buf = buf[bufLen:] |
| 320 | |
| 321 | // Copy everything up until the next backslash |
| 322 | nextBackslash := bytes.IndexByte(in, '\\') |
| 323 | if nextBackslash == -1 { |
| 324 | copy(buf, in) |
| 325 | buf = buf[len(in):] |
| 326 | break |
| 327 | } else { |
| 328 | copy(buf, in[:nextBackslash]) |
| 329 | buf = buf[nextBackslash:] |
| 330 | in = in[nextBackslash:] |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Trim the out buffer to the amount that was actually emitted |
| 335 | return out[:len(out)-len(buf)], nil |
| 336 | } |
no test coverage detected