You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/string_utils.h"
  11. namespace rtc {
  12. size_t strcpyn(char* buffer,
  13. size_t buflen,
  14. const char* source,
  15. size_t srclen /* = SIZE_UNKNOWN */) {
  16. if (buflen <= 0)
  17. return 0;
  18. if (srclen == SIZE_UNKNOWN) {
  19. srclen = strlen(source);
  20. }
  21. if (srclen >= buflen) {
  22. srclen = buflen - 1;
  23. }
  24. memcpy(buffer, source, srclen);
  25. buffer[srclen] = 0;
  26. return srclen;
  27. }
  28. static const char kWhitespace[] = " \n\r\t";
  29. std::string string_trim(const std::string& s) {
  30. std::string::size_type first = s.find_first_not_of(kWhitespace);
  31. std::string::size_type last = s.find_last_not_of(kWhitespace);
  32. if (first == std::string::npos || last == std::string::npos) {
  33. return std::string("");
  34. }
  35. return s.substr(first, last - first + 1);
  36. }
  37. std::string ToHex(const int i) {
  38. char buffer[50];
  39. snprintf(buffer, sizeof(buffer), "%x", i);
  40. return std::string(buffer);
  41. }
  42. std::string LeftPad(char padding, unsigned length, std::string s) {
  43. if (s.length() >= length)
  44. return s;
  45. return std::string(length - s.length(), padding) + s;
  46. }
  47. } // namespace rtc