| Flutter Impeller |

strings.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/base/strings.h"
6
7 #include <cstdarg>
8
9 namespace impeller {
10
11 IMPELLER_PRINTF_FORMAT(1, 2)
12 std::string SPrintF(const char* format, ...) {
13 std::string ret_val;
14 va_list list;
15 va_list list2;
16 va_start(list, format);
17 va_copy(list2, list);
18 if (auto string_length = ::vsnprintf(nullptr, 0, format, list);
19 string_length >= 0) {
20 auto buffer = reinterpret_cast<char*>(::malloc(string_length + 1));
21 ::vsnprintf(buffer, string_length + 1, format, list2);
22 ret_val = std::string{buffer, static_cast<size_t>(string_length)};
23 ::free(buffer);
24 }
25 va_end(list2);
26 va_end(list);
27 return ret_val;
28 }
29
30 bool HasPrefix(const std::string& string, const std::string& prefix) {
31 return string.find(prefix) == 0u;
32 }
33
34 bool HasSuffix(const std::string& string, const std::string& suffix) {
35 auto position = string.rfind(suffix);
36 if (position == std::string::npos) {
37 return false;
38 }
39 return position == string.size() - suffix.size();
40 }
41
42 std::string StripPrefix(const std::string& string,
43 const std::string& to_strip) {
44 if (!HasPrefix(string, to_strip)) {
45 return string;
46 }
47 return string.substr(to_strip.length());
48 }
49
50 } // namespace impeller
IMPELLER_PRINTF_FORMAT
#define IMPELLER_PRINTF_FORMAT(format_number, args_number)
Definition: config.h:22
impeller
Definition: allocation.cc:12
impeller::HasPrefix
bool HasPrefix(const std::string &string, const std::string &prefix)
Definition: strings.cc:30
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::HasSuffix
bool HasSuffix(const std::string &string, const std::string &suffix)
Definition: strings.cc:34
impeller::StripPrefix
std::string StripPrefix(const std::string &string, const std::string &to_strip)
Definition: strings.cc:42
std
Definition: comparable.h:95
strings.h