• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
10 
11 #include "rtc_base/string_utils.h"
12 
13 namespace rtc {
14 
strcpyn(char * buffer,size_t buflen,const char * source,size_t srclen)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 
33 static const char kWhitespace[] = " \n\r\t";
34 
string_trim(const std::string & s)35 std::string string_trim(const std::string& s) {
36   std::string::size_type first = s.find_first_not_of(kWhitespace);
37   std::string::size_type last = s.find_last_not_of(kWhitespace);
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 
ToHex(const int i)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 
LeftPad(char padding,unsigned length,std::string s)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 
59 }  // namespace rtc
60