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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 17 | void replace_substrs(const char* search, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | size_t search_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 19 | const char* replace, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | size_t replace_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | std::string* s) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | size_t pos = 0; |
| 23 | while ((pos = s->find(search, pos, search_len)) != std::string::npos) { |
| 24 | s->replace(pos, search_len, replace, replace_len); |
| 25 | pos += replace_len; |
| 26 | } |
| 27 | } |
| 28 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | bool starts_with(const char* s1, const char* s2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | return strncmp(s1, s2, strlen(s2)) == 0; |
| 31 | } |
| 32 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | bool ends_with(const char* s1, const char* s2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | size_t s1_length = strlen(s1); |
| 35 | size_t s2_length = strlen(s2); |
| 36 | |
| 37 | if (s2_length > s1_length) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | const char* start = s1 + (s1_length - s2_length); |
| 42 | return strncmp(start, s2, s2_length) == 0; |
| 43 | } |
| 44 | |
| 45 | static const char kWhitespace[] = " \n\r\t"; |
| 46 | |
| 47 | std::string string_trim(const std::string& s) { |
| 48 | std::string::size_type first = s.find_first_not_of(kWhitespace); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | std::string::size_type last = s.find_last_not_of(kWhitespace); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
| 51 | if (first == std::string::npos || last == std::string::npos) { |
| 52 | return std::string(""); |
| 53 | } |
| 54 | |
| 55 | return s.substr(first, last - first + 1); |
| 56 | } |
| 57 | |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 58 | std::string ToHex(const int i) { |
| 59 | char buffer[50]; |
| 60 | snprintf(buffer, sizeof(buffer), "%x", i); |
| 61 | |
| 62 | return std::string(buffer); |
| 63 | } |
| 64 | |
Jonas Olsson | d8c5078 | 2018-09-07 11:21:28 +0200 | [diff] [blame] | 65 | std::string LeftPad(char padding, unsigned length, std::string s) { |
| 66 | if (s.length() >= length) |
| 67 | return s; |
| 68 | return std::string(length - s.length(), padding) + s; |
| 69 | } |
| 70 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | } // namespace rtc |