1 // Copyright (c) 2013 The Chromium 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.
5 #include "tools/gn/string_utils.h"
7 #include "tools/gn/err.h"
8 #include "tools/gn/scope.h"
9 #include "tools/gn/token.h"
10 #include "tools/gn/tokenizer.h"
11 #include "tools/gn/value.h"
15 // Constructs an Err indicating a range inside a string. We assume that the
16 // token has quotes around it that are not counted by the offset.
17 Err ErrInsideStringToken(const Token& token, size_t offset, size_t size,
18 const std::string& msg,
19 const std::string& help = std::string()) {
20 // The "+1" is skipping over the " at the beginning of the token.
21 int int_offset = static_cast<int>(offset);
22 Location begin_loc(token.location().file(),
23 token.location().line_number(),
24 token.location().char_offset() + int_offset + 1,
25 token.location().byte() + int_offset + 1);
27 token.location().file(),
28 token.location().line_number(),
29 token.location().char_offset() + int_offset + 1 + static_cast<int>(size),
30 token.location().byte() + int_offset + 1 + static_cast<int>(size));
31 return Err(LocationRange(begin_loc, end_loc), msg, help);
34 // Given the character input[i] indicating the $ in a string, locates the
35 // identifier and places its range in |*identifier|, and updates |*i| to
36 // point to the last character consumed.
38 // On error returns false and sets the error.
39 bool LocateInlineIdenfitier(const Token& token,
40 const char* input, size_t size,
42 base::StringPiece* identifier,
44 size_t dollars_index = *i;
47 *err = ErrInsideStringToken(token, dollars_index, 1, "$ at end of string.",
48 "I was expecting an identifier after the $.");
53 if (input[*i] == '{') {
56 *err = ErrInsideStringToken(token, dollars_index, 2,
57 "${ at end of string.",
58 "I was expecting an identifier inside the ${...}.");
66 // First char is special.
67 if (!Tokenizer::IsIdentifierFirstChar(input[*i])) {
68 *err = ErrInsideStringToken(
69 token, dollars_index, *i - dollars_index + 1,
70 "$ not followed by an identifier char.",
71 "It you want a literal $ use \"\\$\".");
74 size_t begin_offset = *i;
77 // Find the first non-identifier char following the string.
78 while (*i < size && Tokenizer::IsIdentifierContinuingChar(input[*i]))
80 size_t end_offset = *i;
82 // If we started with a bracket, validate that there's an ending one. Leave
83 // *i pointing to the last char we consumed (backing up one).
86 *err = ErrInsideStringToken(token, dollars_index, *i - dollars_index,
87 "Unterminated ${...");
89 } else if (input[*i] != '}') {
90 *err = ErrInsideStringToken(token, *i, 1, "Not an identifier in string expansion.",
91 "The contents of ${...} should be an identifier. "
92 "This character is out of sorts.");
95 // We want to consume the bracket but also back up one, so *i is unchanged.
100 *identifier = base::StringPiece(&input[begin_offset],
101 end_offset - begin_offset);
105 bool AppendIdentifierValue(Scope* scope,
107 const base::StringPiece& identifier,
110 const Value* value = scope->GetValue(identifier, true);
112 // We assume the identifier points inside the token.
113 *err = ErrInsideStringToken(
114 token, identifier.data() - token.value().data() - 1, identifier.size(),
115 "Undefined identifier in string expansion.",
116 std::string("\"") + identifier + "\" is not currently in scope.");
120 output->append(value->ToString(false));
126 bool ExpandStringLiteral(Scope* scope,
127 const Token& literal,
130 DCHECK(literal.type() == Token::STRING);
131 DCHECK(literal.value().size() > 1); // Should include quotes.
132 DCHECK(result->type() == Value::STRING); // Should be already set.
134 // The token includes the surrounding quotes, so strip those off.
135 const char* input = &literal.value().data()[1];
136 size_t size = literal.value().size() - 2;
138 std::string& output = result->string_value();
139 output.reserve(size);
140 for (size_t i = 0; i < size; i++) {
141 if (input[i] == '\\') {
143 switch (input[i + 1]) {
147 output.push_back(input[i + 1]);
150 default: // Everything else has no meaning: pass the literal.
154 output.push_back(input[i]);
155 } else if (input[i] == '$') {
156 base::StringPiece identifier;
157 if (!LocateInlineIdenfitier(literal, input, size, &i, &identifier, err))
159 if (!AppendIdentifierValue(scope, literal, identifier, &output, err))
162 output.push_back(input[i]);
168 std::string RemovePrefix(const std::string& str, const std::string& prefix) {
169 CHECK(str.size() >= prefix.size() &&
170 str.compare(0, prefix.size(), prefix) == 0);
171 return str.substr(prefix.size());
174 void TrimTrailingSlash(std::string* str) {
176 DCHECK((*str)[str->size() - 1] == '/');
177 str->resize(str->size() - 1);