Shaka Player Embedded
utils.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/util/utils.h"
16 
17 #include <glog/logging.h>
18 
19 #include <algorithm>
20 #include <cctype>
21 
22 namespace shaka {
23 namespace util {
24 
25 namespace {
26 
27 PRINTF_FORMAT(2, 0)
28 void StringAppendV(std::string* dest, const char* format, va_list ap) {
29  // Get the required size of the buffer.
30  va_list ap_copy;
31  va_copy(ap_copy, ap);
32  int size = vsnprintf(nullptr, 0, format, ap_copy); // NOLINT
33  va_end(ap_copy);
34 
35  PCHECK(size >= 0);
36  std::string buffer(size + 1, '\0');
37  CHECK_EQ(size, vsnprintf(&buffer[0], buffer.size(), format, ap));
38 
39  // Drop the '\0' that was added by vsnprintf, std::string has an implicit '\0'
40  // after the string.
41  buffer.resize(size);
42 
43  dest->append(buffer);
44 }
45 
46 } // namespace
47 
48 
49 std::string StringPrintf(const char* format, ...) { // NOLINT(cert-dcl50-cpp)
50  va_list ap;
51  va_start(ap, format);
52  std::string result;
53  StringAppendV(&result, format, ap);
54  va_end(ap);
55  return result;
56 }
57 
58 std::string StringPrintfV(const char* format, va_list va) {
59  std::string result;
60  StringAppendV(&result, format, va);
61  return result;
62 }
63 
64 std::vector<std::string> StringSplit(const std::string& source, char split_on) {
65  std::vector<std::string> split;
66  std::string::size_type start = 0;
67  std::string::size_type end = 0;
68  while ((end = source.find(split_on, start)) != std::string::npos) {
69  split.push_back(source.substr(start, end - start));
70  start = end + 1;
71  }
72  split.push_back(source.substr(start));
73  return split;
74 }
75 
76 std::string ToAsciiLower(const std::string& source) {
77  std::string ret = source;
78  std::transform(source.begin(), source.end(), ret.begin(), ::tolower);
79  return ret;
80 }
81 
82 std::string TrimAsciiWhitespace(const std::string& source) {
83  size_t start = 0;
84  size_t end = source.size();
85  while (start < source.size() && std::isspace(source[start]))
86  ++start;
87  if (start == source.size())
88  return "";
89 
90  while (end > 0 && std::isspace(source[end - 1]))
91  --end;
92 
93  DCHECK_LT(start, end);
94  return source.substr(start, end - start);
95 }
96 
97 std::string ToHexString(const uint8_t* data, size_t data_size) {
98  const char kCharMap[] = "0123456789ABCDEF";
99  std::string ret(data_size * 2, '\0');
100  DCHECK_EQ(ret.size(), data_size * 2);
101  for (size_t i = 0; i < data_size; i++) {
102  ret[i * 2] = kCharMap[data[i] >> 4];
103  ret[i * 2 + 1] = kCharMap[data[i] & 0xf];
104  }
105  return ret;
106 }
107 
108 } // namespace util
109 } // namespace shaka
std::string ToAsciiLower(const std::string &source)
Definition: utils.cc:76
const char * dest
Definition: media_utils.cc:31
std::string ToHexString(const uint8_t *data, size_t data_size)
Definition: utils.cc:97
std::string StringPrintf(const char *format,...)
Definition: utils.cc:49
const char * source
Definition: media_utils.cc:30
std::string StringPrintfV(const char *format, va_list va)
Definition: utils.cc:58
std::string TrimAsciiWhitespace(const std::string &source)
Definition: utils.cc:82
std::vector< std::string > StringSplit(const std::string &source, char split_on)
Definition: utils.cc:64
#define PRINTF_FORMAT(format, dots)
Definition: macros.h:39