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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/string_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | namespace rtc { |
| 14 | |
Niels Möller | d189252 | 2018-10-17 13:39:01 +0200 | [diff] [blame] | 15 | size_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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | static const char kWhitespace[] = " \n\r\t"; |
| 34 | |
| 35 | std::string string_trim(const std::string& s) { |
| 36 | std::string::size_type first = s.find_first_not_of(kWhitespace); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | std::string::size_type last = s.find_last_not_of(kWhitespace); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | |
| 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 Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 46 | std::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 Olsson | d8c5078 | 2018-09-07 11:21:28 +0200 | [diff] [blame] | 53 | std::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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | } // namespace rtc |