string.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 trace
  8. {
  9. namespace propagation
  10. {
  11. // NOTE - code within `detail` namespace implements internal details, and not part
  12. // of the public interface.
  13. namespace detail
  14. {
  15. /**
  16. * Splits a string by separator, up to given buffer count words.
  17. * Returns the amount of words the input was split into.
  18. */
  19. inline size_t SplitString(nostd::string_view s,
  20. char separator,
  21. nostd::string_view *results,
  22. size_t count)
  23. {
  24. if (count == 0)
  25. {
  26. return count;
  27. }
  28. size_t filled = 0;
  29. size_t token_start = 0;
  30. for (size_t i = 0; i < s.size(); i++)
  31. {
  32. if (s[i] != separator)
  33. {
  34. continue;
  35. }
  36. results[filled++] = s.substr(token_start, i - token_start);
  37. if (filled == count)
  38. {
  39. return count;
  40. }
  41. token_start = i + 1;
  42. }
  43. if (filled < count)
  44. {
  45. results[filled++] = s.substr(token_start);
  46. }
  47. return filled;
  48. }
  49. } // namespace detail
  50. } // namespace propagation
  51. } // namespace trace
  52. OPENTELEMETRY_END_NAMESPACE