| 464 | } |
| 465 | |
| 466 | void OrderedCode::WriteSignedNumIncreasing(string* dest, int64 val) { |
| 467 | const uint64 x = val < 0 ? ~val : val; |
| 468 | if (x < 64) { // fast path for encoding length == 1 |
| 469 | *dest += kLengthToHeaderBits[1][0] ^ val; |
| 470 | return; |
| 471 | } |
| 472 | // buf = val in network byte order, sign extended to 10 bytes |
| 473 | const char sign_byte = val < 0 ? '\xff' : '\0'; |
| 474 | char buf[10] = { |
| 475 | sign_byte, |
| 476 | sign_byte, |
| 477 | }; |
| 478 | StoreBigEndian64(buf + 2, val); |
| 479 | static_assert(sizeof(buf) == kMaxSigned64Length, "max length size mismatch"); |
| 480 | const int len = SignedEncodingLength(x); |
| 481 | DCHECK_GE(len, 2); |
| 482 | char* const begin = buf + sizeof(buf) - len; |
| 483 | begin[0] ^= kLengthToHeaderBits[len][0]; |
| 484 | begin[1] ^= kLengthToHeaderBits[len][1]; // ok because len >= 2 |
| 485 | dest->append(begin, len); |
| 486 | } |
| 487 | |
| 488 | bool OrderedCode::ReadSignedNumIncreasing(StringPiece* src, int64* result) { |
| 489 | if (src->empty()) return false; |
nothing calls this directly
no test coverage detected