PBUtility.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "MMBuffer.h"
  21. #include "PBUtility.h"
  22. namespace mmkv {
  23. uint32_t pbRawVarint32Size(uint32_t value) {
  24. if ((value & (0xffffffff << 7)) == 0) {
  25. return 1;
  26. } else if ((value & (0xffffffff << 14)) == 0) {
  27. return 2;
  28. } else if ((value & (0xffffffff << 21)) == 0) {
  29. return 3;
  30. } else if ((value & (0xffffffff << 28)) == 0) {
  31. return 4;
  32. }
  33. return 5;
  34. }
  35. uint32_t pbUInt64Size(uint64_t value) {
  36. if ((value & (0xffffffffffffffffL << 7)) == 0) {
  37. return 1;
  38. } else if ((value & (0xffffffffffffffffL << 14)) == 0) {
  39. return 2;
  40. } else if ((value & (0xffffffffffffffffL << 21)) == 0) {
  41. return 3;
  42. } else if ((value & (0xffffffffffffffffL << 28)) == 0) {
  43. return 4;
  44. } else if ((value & (0xffffffffffffffffL << 35)) == 0) {
  45. return 5;
  46. } else if ((value & (0xffffffffffffffffL << 42)) == 0) {
  47. return 6;
  48. } else if ((value & (0xffffffffffffffffL << 49)) == 0) {
  49. return 7;
  50. } else if ((value & (0xffffffffffffffffL << 56)) == 0) {
  51. return 8;
  52. } else if ((value & (0xffffffffffffffffL << 63)) == 0) {
  53. return 9;
  54. }
  55. return 10;
  56. }
  57. } // namespace mmkv