helpers.cpp 1.0 KB

123456789101112131415161718192021222324252627282930
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lex/helpers.h"
  5. namespace Carbon::Lex {
  6. auto CanLexInt(Diagnostics::Emitter<const char*>& emitter, llvm::StringRef text)
  7. -> bool {
  8. // Integer parsing has poor scaling characteristics for extremely large digit
  9. // amounts. We've done some performance work on this, but this limit exists to
  10. // avoid really extreme cases.
  11. //
  12. // 2^128 would be 39 decimal digits or 128 binary. In either case, this limit
  13. // is far above the threshold for normal ints.
  14. constexpr size_t DigitLimit = 10000;
  15. if (text.size() > DigitLimit) {
  16. CARBON_DIAGNOSTIC(
  17. TooManyDigits, Error,
  18. "found a sequence of {0} digits, which is greater than the "
  19. "limit of {1}",
  20. size_t, size_t);
  21. emitter.Emit(text.begin(), TooManyDigits, text.size(), DigitLimit);
  22. return false;
  23. }
  24. return true;
  25. }
  26. } // namespace Carbon::Lex