Flutter Impeller
utilities.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 
6 
7 #include <cctype>
8 #include <filesystem>
9 #include <sstream>
10 
11 namespace impeller {
12 namespace compiler {
13 
14 std::string Utf8FromPath(const std::filesystem::path& path) {
15  return reinterpret_cast<const char*>(path.u8string().c_str());
16 }
17 
18 std::string InferShaderNameFromPath(std::string_view path) {
19  auto p = std::filesystem::path{path}.stem();
20  return Utf8FromPath(p);
21 }
22 
23 std::string ConvertToCamelCase(std::string_view string) {
24  if (string.empty()) {
25  return "";
26  }
27 
28  std::stringstream stream;
29  bool next_upper = true;
30  for (size_t i = 0, count = string.length(); i < count; i++) {
31  auto ch = string.data()[i];
32  if (next_upper) {
33  next_upper = false;
34  stream << static_cast<char>(std::toupper(ch));
35  continue;
36  }
37  if (ch == '_') {
38  next_upper = true;
39  continue;
40  }
41  stream << ch;
42  }
43  return stream.str();
44 }
45 
46 std::string ConvertToEntrypointName(std::string_view string) {
47  if (string.empty()) {
48  return "";
49  }
50  std::stringstream stream;
51  // Append a prefix if the first character is not a letter.
52  if (!std::isalpha(string.data()[0])) {
53  stream << "i_";
54  }
55  for (size_t i = 0, count = string.length(); i < count; i++) {
56  auto ch = string.data()[i];
57  if (std::isalnum(ch) || ch == '_') {
58  stream << ch;
59  }
60  }
61  return stream.str();
62 }
63 
64 bool StringStartsWith(const std::string& target, const std::string& prefix) {
65  if (prefix.length() > target.length()) {
66  return false;
67  }
68  for (size_t i = 0; i < prefix.length(); i++) {
69  if (target[i] != prefix[i]) {
70  return false;
71  }
72  }
73  return true;
74 }
75 
76 } // namespace compiler
77 } // namespace impeller
impeller::compiler::ConvertToEntrypointName
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:46
impeller::compiler::InferShaderNameFromPath
std::string InferShaderNameFromPath(std::string_view path)
Definition: utilities.cc:18
impeller::compiler::ConvertToCamelCase
std::string ConvertToCamelCase(std::string_view string)
Definition: utilities.cc:23
utilities.h
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:14
impeller::compiler::StringStartsWith
bool StringStartsWith(const std::string &target, const std::string &prefix)
Definition: utilities.cc:64
impeller
Definition: aiks_context.cc:10