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