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 "PBUtility.h"
  21. namespace mmkv {
  22. uint32_t pbRawVarint32Size(uint32_t value) {
  23. if ((value & (0xffffffff << 7)) == 0) {
  24. return 1;
  25. } else if ((value & (0xffffffff << 14)) == 0) {
  26. return 2;
  27. } else if ((value & (0xffffffff << 21)) == 0) {
  28. return 3;
  29. } else if ((value & (0xffffffff << 28)) == 0) {
  30. return 4;
  31. }
  32. return 5;
  33. }
  34. uint32_t pbUInt64Size(uint64_t value) {
  35. if ((value & (0xffffffffffffffffL << 7)) == 0) {
  36. return 1;
  37. } else if ((value & (0xffffffffffffffffL << 14)) == 0) {
  38. return 2;
  39. } else if ((value & (0xffffffffffffffffL << 21)) == 0) {
  40. return 3;
  41. } else if ((value & (0xffffffffffffffffL << 28)) == 0) {
  42. return 4;
  43. } else if ((value & (0xffffffffffffffffL << 35)) == 0) {
  44. return 5;
  45. } else if ((value & (0xffffffffffffffffL << 42)) == 0) {
  46. return 6;
  47. } else if ((value & (0xffffffffffffffffL << 49)) == 0) {
  48. return 7;
  49. } else if ((value & (0xffffffffffffffffL << 56)) == 0) {
  50. return 8;
  51. } else if ((value & (0xffffffffffffffffL << 63)) == 0) {
  52. return 9;
  53. }
  54. return 10;
  55. }
  56. } // namespace mmkv