blob: dfbb548050c6ee289898066406420cdc1c5dbe2a [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/string_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013namespace rtc {
14
Niels Möllerd1892522018-10-17 13:39:01 +020015size_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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033static const char kWhitespace[] = " \n\r\t";
34
35std::string string_trim(const std::string& s) {
36 std::string::size_type first = s.find_first_not_of(kWhitespace);
Yves Gerey665174f2018-06-19 15:03:05 +020037 std::string::size_type last = s.find_last_not_of(kWhitespace);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
39 if (first == std::string::npos || last == std::string::npos) {
40 return std::string("");
41 }
42
43 return s.substr(first, last - first + 1);
44}
45
Jonas Olsson74395342018-04-03 12:22:07 +020046std::string ToHex(const int i) {
47 char buffer[50];
48 snprintf(buffer, sizeof(buffer), "%x", i);
49
50 return std::string(buffer);
51}
52
Jonas Olssond8c50782018-09-07 11:21:28 +020053std::string LeftPad(char padding, unsigned length, std::string s) {
54 if (s.length() >= length)
55 return s;
56 return std::string(length - s.length(), padding) + s;
57}
58
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059} // namespace rtc