1 // Copyright 2020 The Amber Authors.
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 // http://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/virtual_file_store.h"
16
17 namespace amber {
18 namespace {
19
HasPrefix(const std::string & str,const std::string & prefix)20 bool HasPrefix(const std::string& str, const std::string& prefix) {
21 return str.compare(0, prefix.size(), prefix) == 0;
22 }
23
TrimPrefix(const std::string & str,const std::string & prefix)24 std::string TrimPrefix(const std::string& str, const std::string& prefix) {
25 return HasPrefix(str, prefix) ? str.substr(prefix.length()) : str;
26 }
27
ReplaceAll(std::string str,const std::string & substr,const std::string & replacement)28 std::string ReplaceAll(std::string str,
29 const std::string& substr,
30 const std::string& replacement) {
31 size_t pos = 0;
32 while ((pos = str.find(substr, pos)) != std::string::npos) {
33 str.replace(pos, substr.length(), replacement);
34 pos += replacement.length();
35 }
36 return str;
37 }
38
39 } // namespace
40
GetCanonical(const std::string & path)41 std::string VirtualFileStore::GetCanonical(const std::string& path) {
42 auto canonical = path;
43 canonical = ReplaceAll(canonical, "\\", "/");
44 canonical = TrimPrefix(canonical, "./");
45 return canonical;
46 }
47
48 } // namespace amber
49