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 <algorithm>
8 #include <cctype>
9 #include <filesystem>
10 #include <iostream>
11 #include <sstream>
12 
13 namespace impeller {
14 namespace compiler {
15 
16 bool SetPermissiveAccess(const std::filesystem::path& p) {
17  auto permissions =
18  std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
19  std::filesystem::perms::group_read | std::filesystem::perms::others_read;
20  std::error_code error;
21  std::filesystem::permissions(p, permissions, error);
22  if (error) {
23  std::cerr << "Failed to set access on file '" << p
24  << "': " << error.message() << std::endl;
25  return false;
26  }
27  return true;
28 }
29 
30 std::string Utf8FromPath(const std::filesystem::path& path) {
31  return reinterpret_cast<const char*>(path.u8string().c_str());
32 }
33 
34 std::string InferShaderNameFromPath(const std::filesystem::path& path) {
35  return Utf8FromPath(path.stem());
36 }
37 
38 std::string ToCamelCase(std::string_view string) {
39  if (string.empty()) {
40  return "";
41  }
42 
43  std::stringstream stream;
44  bool next_upper = true;
45  for (size_t i = 0, count = string.length(); i < count; i++) {
46  auto ch = string.data()[i];
47  if (next_upper) {
48  next_upper = false;
49  stream << static_cast<char>(std::toupper(ch));
50  continue;
51  }
52  if (ch == '_') {
53  next_upper = true;
54  continue;
55  }
56  stream << ch;
57  }
58  return stream.str();
59 }
60 
61 std::string ToLowerCase(std::string_view string) {
62  std::string result = std::string(string);
63  std::transform(result.begin(), result.end(), result.begin(),
64  [](char x) { return std::tolower(x); });
65  return result;
66 }
67 
68 std::string ConvertToEntrypointName(std::string_view string) {
69  if (string.empty()) {
70  return "";
71  }
72  std::stringstream stream;
73  // Append a prefix if the first character is not a letter.
74  if (!std::isalpha(string.data()[0])) {
75  stream << "i_";
76  }
77  for (size_t i = 0, count = string.length(); i < count; i++) {
78  auto ch = string.data()[i];
79  if (std::isalnum(ch) || ch == '_') {
80  stream << ch;
81  }
82  }
83  return stream.str();
84 }
85 
86 bool StringStartsWith(const std::string& target, const std::string& prefix) {
87  if (prefix.length() > target.length()) {
88  return false;
89  }
90  for (size_t i = 0; i < prefix.length(); i++) {
91  if (target[i] != prefix[i]) {
92  return false;
93  }
94  }
95  return true;
96 }
97 
98 } // namespace compiler
99 } // namespace impeller
int32_t x
std::string ConvertToEntrypointName(std::string_view string)
Ensure that the entrypoint name is a valid identifier in the target language.
Definition: utilities.cc:68
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:61
bool SetPermissiveAccess(const std::filesystem::path &p)
Sets the file access mode of the file at path 'p' to 0644.
Definition: utilities.cc:16
std::string InferShaderNameFromPath(const std::filesystem::path &path)
Definition: utilities.cc:34
std::string ToCamelCase(std::string_view string)
Definition: utilities.cc:38
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
bool StringStartsWith(const std::string &target, const std::string &prefix)
Definition: utilities.cc:86
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:69