| 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 |
| 11 | #include "rtc_base/stringutils.h" |
| 12 |
| 13 | namespace rtc { |
| 14 |
| 15 | size_t strcpyn(char* buffer, |
| 16 | size_t buflen, |
| 17 | const char* source, |
| 18 | size_t srclen /* = SIZE_UNKNOWN */) { |
| 19 | if (buflen <= 0) |
| 20 | return 0; |
| 21 |
| 22 | if (srclen == SIZE_UNKNOWN) { |
| 23 | srclen = strlen(source); |
| 24 | } |
| 25 | if (srclen >= buflen) { |
| 26 | srclen = buflen - 1; |
| 27 | } |
| 28 | memcpy(buffer, source, srclen); |
| 29 | buffer[srclen] = 0; |
| 30 | return srclen; |
| 31 | } |
| 32 |
| 33 | void replace_substrs(const char* search, |
| 34 | size_t search_len, |
| 35 | const char* replace, |
| 36 | size_t replace_len, |
| 37 | std::string* s) { |
| 38 | size_t pos = 0; |
| 39 | while ((pos = s->find(search, pos, search_len)) != std::string::npos) { |
| 40 | s->replace(pos, search_len, replace, replace_len); |
| 41 | pos += replace_len; |
| 42 | } |
| 43 | } |
| 44 |
| 45 | bool starts_with(const char* s1, const char* s2) { |
| 46 | return strncmp(s1, s2, strlen(s2)) == 0; |
| 47 | } |
| 48 |
| 49 | bool ends_with(const char* s1, const char* s2) { |
| 50 | size_t s1_length = strlen(s1); |
| 51 | size_t s2_length = strlen(s2); |
| 52 |
| 53 | if (s2_length > s1_length) { |
| 54 | return false; |
| 55 | } |
| 56 |
| 57 | const char* start = s1 + (s1_length - s2_length); |
| 58 | return strncmp(start, s2, s2_length) == 0; |
| 59 | } |
| 60 |
| 61 | static const char kWhitespace[] = " \n\r\t"; |
| 62 |
| 63 | std::string string_trim(const std::string& s) { |
| 64 | std::string::size_type first = s.find_first_not_of(kWhitespace); |
| 65 | std::string::size_type last = s.find_last_not_of(kWhitespace); |
| 66 |
| 67 | if (first == std::string::npos || last == std::string::npos) { |
| 68 | return std::string(""); |
| 69 | } |
| 70 |
| 71 | return s.substr(first, last - first + 1); |
| 72 | } |
| 73 |
| 74 | std::string ToHex(const int i) { |
| 75 | char buffer[50]; |
| 76 | snprintf(buffer, sizeof(buffer), "%x", i); |
| 77 |
| 78 | return std::string(buffer); |
| 79 | } |
| 80 |
| 81 | std::string LeftPad(char padding, unsigned length, std::string s) { |
| 82 | if (s.length() >= length) |
| 83 | return s; |
| 84 | return std::string(length - s.length(), padding) + s; |
| 85 | } |
| 86 |
| 87 | } // namespace rtc |
| 88 |