| 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
5 #include "impeller/compiler/utilities.h"
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(std::string_view path) {
35 auto p = std::filesystem::path{path}.stem();
36 return Utf8FromPath(p);
37 }
38
39 std::string ToCamelCase(std::string_view string) {
40 if (string.empty()) {
41 return "";
42 }
43
44 std::stringstream stream;
45 bool next_upper = true;
46 for (size_t i = 0, count = string.length(); i < count; i++) {
47 auto ch = string.data()[i];
48 if (next_upper) {
49 next_upper = false;
50 stream << static_cast<char>(std::toupper(ch));
51 continue;
52 }
53 if (ch == '_') {
54 next_upper = true;
55 continue;
56 }
57 stream << ch;
58 }
59 return stream.str();
60 }
61
62 std::string ToLowerCase(std::string_view string) {
63 std::string result = std::string(string);
64 std::transform(result.begin(), result.end(), result.begin(),
65 [](char x) { return std::tolower(x); });
66 return result;
67 }
68
69 std::string ConvertToEntrypointName(std::string_view string) {
70 if (string.empty()) {
71 return "";
72 }
73 std::stringstream stream;
74 // Append a prefix if the first character is not a letter.
75 if (!std::isalpha(string.data()[0])) {
76 stream << "i_";
77 }
78 for (size_t i = 0, count = string.length(); i < count; i++) {
79 auto ch = string.data()[i];
80 if (std::isalnum(ch) || ch == '_') {
81 stream << ch;
82 }
83 }
84 return stream.str();
85 }
86
87 bool StringStartsWith(const std::string& target, const std::string& prefix) {
88 if (prefix.length() > target.length()) {
89 return false;
90 }
91 for (size_t i = 0; i < prefix.length(); i++) {
92 if (target[i] != prefix[i]) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 } // namespace compiler
100 } // namespace impeller
impeller::compiler::ToCamelCase
std::string ToCamelCase(std::string_view string)
Definition: utilities.cc:39
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:69
impeller::compiler::ToLowerCase
std::string ToLowerCase(std::string_view string)
Definition: utilities.cc:62
impeller::compiler::InferShaderNameFromPath
std::string InferShaderNameFromPath(std::string_view path)
Definition: utilities.cc:34
utilities.h
impeller::compiler::SetPermissiveAccess
bool SetPermissiveAccess(const std::filesystem::path &p)
Sets the file access mode of the file at path 'p' to 0644.
Definition: utilities.cc:16
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
impeller::compiler::StringStartsWith
bool StringStartsWith(const std::string &target, const std::string &prefix)
Definition: utilities.cc:87
impeller
Definition: aiks_blur_unittests.cc:20