blob: db48ec722ff17952cf3514a022ada69dfa20c0d0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Gerey2e00abc2018-10-05 15:39:24 +020010
11#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
15namespace rtc {
16
Yves Gerey665174f2018-06-19 15:03:05 +020017void replace_substrs(const char* search,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018 size_t search_len,
Yves Gerey665174f2018-06-19 15:03:05 +020019 const char* replace,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020 size_t replace_len,
Yves Gerey665174f2018-06-19 15:03:05 +020021 std::string* s) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 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 Gerey665174f2018-06-19 15:03:05 +020029bool starts_with(const char* s1, const char* s2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 return strncmp(s1, s2, strlen(s2)) == 0;
31}
32
Yves Gerey665174f2018-06-19 15:03:05 +020033bool ends_with(const char* s1, const char* s2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 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
45static const char kWhitespace[] = " \n\r\t";
46
47std::string string_trim(const std::string& s) {
48 std::string::size_type first = s.find_first_not_of(kWhitespace);
Yves Gerey665174f2018-06-19 15:03:05 +020049 std::string::size_type last = s.find_last_not_of(kWhitespace);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
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 Olsson74395342018-04-03 12:22:07 +020058std::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 Olssond8c50782018-09-07 11:21:28 +020065std::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.orgf0488722014-05-13 18:00:26 +000071} // namespace rtc