string_util.h 773 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/nostd/string_view.h"
  5. #include "opentelemetry/version.h"
  6. OPENTELEMETRY_BEGIN_NAMESPACE
  7. namespace common
  8. {
  9. class StringUtil
  10. {
  11. public:
  12. static nostd::string_view Trim(nostd::string_view str, size_t left, size_t right) noexcept
  13. {
  14. while (left <= right && isspace(str[left]))
  15. {
  16. left++;
  17. }
  18. while (left <= right && isspace(str[right]))
  19. {
  20. right--;
  21. }
  22. return str.substr(left, 1 + right - left);
  23. }
  24. static nostd::string_view Trim(nostd::string_view str) noexcept
  25. {
  26. if (str.empty())
  27. {
  28. return str;
  29. }
  30. return Trim(str, 0, str.size() - 1);
  31. }
  32. };
  33. } // namespace common
  34. OPENTELEMETRY_END_NAMESPACE