blob: 333c06073831f1a9f70b67efd6d172fdccf549bf [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
Niels Möllerd1892522018-10-17 13:39:01 +020017size_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 Gerey665174f2018-06-19 15:03:05 +020035void replace_substrs(const char* search,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036 size_t search_len,
Yves Gerey665174f2018-06-19 15:03:05 +020037 const char* replace,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 size_t replace_len,
Yves Gerey665174f2018-06-19 15:03:05 +020039 std::string* s) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 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 Gerey665174f2018-06-19 15:03:05 +020047bool starts_with(const char* s1, const char* s2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 return strncmp(s1, s2, strlen(s2)) == 0;
49}
50
Yves Gerey665174f2018-06-19 15:03:05 +020051bool ends_with(const char* s1, const char* s2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 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
63static const char kWhitespace[] = " \n\r\t";
64
65std::string string_trim(const std::string& s) {
66 std::string::size_type first = s.find_first_not_of(kWhitespace);
Yves Gerey665174f2018-06-19 15:03:05 +020067 std::string::size_type last = s.find_last_not_of(kWhitespace);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
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 Olsson74395342018-04-03 12:22:07 +020076std::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 Olssond8c50782018-09-07 11:21:28 +020083std::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.orgf0488722014-05-13 18:00:26 +000089} // namespace rtc