blob: 1720c62d5ef69b8dae7758b7aefdf3954ca942db [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 13:39:2410
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/string_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
henrike@webrtc.orgf0488722014-05-13 18:00:2613namespace rtc {
14
Niels Möllerd1892522018-10-17 11:39:0115size_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:2633static 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 13:03:0537 std::string::size_type last = s.find_last_not_of(kWhitespace);
henrike@webrtc.orgf0488722014-05-13 18:00:2638
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 10:22:0746std::string ToHex(const int i) {
47 char buffer[50];
48 snprintf(buffer, sizeof(buffer), "%x", i);
49
50 return std::string(buffer);
51}
52
henrike@webrtc.orgf0488722014-05-13 18:00:2653} // namespace rtc