lexing.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "util/misc/lexing.h"
  15. #include <ctype.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <limits>
  20. #include "base/strings/string_number_conversions.h"
  21. #include "base/strings/string_piece.h"
  22. namespace crashpad {
  23. namespace {
  24. #define MAKE_ADAPTER(type, function) \
  25. bool ConvertStringToNumber(const base::StringPiece& input, type* value) { \
  26. return function(input, value); \
  27. }
  28. MAKE_ADAPTER(int, base::StringToInt);
  29. MAKE_ADAPTER(unsigned int, base::StringToUint);
  30. MAKE_ADAPTER(int64_t, base::StringToInt64);
  31. MAKE_ADAPTER(uint64_t, base::StringToUint64);
  32. #undef MAKE_ADAPTER
  33. } // namespace
  34. bool AdvancePastPrefix(const char** input, const char* pattern) {
  35. size_t length = strlen(pattern);
  36. if (strncmp(*input, pattern, length) == 0) {
  37. *input += length;
  38. return true;
  39. }
  40. return false;
  41. }
  42. template <typename T>
  43. bool AdvancePastNumber(const char** input, T* value) {
  44. size_t length = 0;
  45. if (std::numeric_limits<T>::is_signed && **input == '-') {
  46. ++length;
  47. }
  48. while (isdigit((*input)[length])) {
  49. ++length;
  50. }
  51. bool success =
  52. ConvertStringToNumber(base::StringPiece(*input, length), value);
  53. if (success) {
  54. *input += length;
  55. return true;
  56. }
  57. return false;
  58. }
  59. template bool AdvancePastNumber(const char** input, int* value);
  60. template bool AdvancePastNumber(const char** input, unsigned int* value);
  61. template bool AdvancePastNumber(const char** input, int64_t* value);
  62. template bool AdvancePastNumber(const char** input, uint64_t* value);
  63. } // namespace crashpad