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. 4 5 #ifndef TOOLS_GN_STRING_UTILS_H_ 6 #define TOOLS_GN_STRING_UTILS_H_ 7 8 #include <string> 9 #include <string_view> 10 #include <vector> 11 12 class Err; 13 class Scope; 14 class Token; 15 class Value; 16 17 inline std::string operator+(const std::string& a, std::string_view b) { 18 std::string ret; 19 ret.reserve(a.size() + b.size()); 20 ret.assign(a); 21 ret.append(b.data(), b.size()); 22 return ret; 23 } 24 25 inline std::string operator+(std::string_view a, const std::string& b) { 26 std::string ret; 27 ret.reserve(a.size() + b.size()); 28 ret.assign(a.data(), a.size()); 29 ret.append(b); 30 return ret; 31 } 32 33 // Unescapes and expands variables in the given literal, writing the result 34 // to the given value. On error, sets |err| and returns false. 35 bool ExpandStringLiteral(Scope* scope, 36 const Token& literal, 37 Value* result, 38 Err* err); 39 40 // Returns the minimum number of inserts, deleted, and replacements of 41 // characters needed to transform s1 to s2, or max_edit_distance + 1 if 42 // transforming s1 into s2 isn't possible in at most max_edit_distance steps. 43 size_t EditDistance(std::string_view s1, 44 std::string_view s2, 45 size_t max_edit_distance); 46 47 // Given a string |text| and a vector of correctly-spelled strings |words|, 48 // returns the first string in |words| closest to |text|, or an empty 49 // std::string_view if none of the strings in |words| is close. 50 std::string_view SpellcheckString(std::string_view text, 51 const std::vector<std::string_view>& words); 52 53 // Reads stdin until end-of-data and returns what it read. 54 std::string ReadStdin(); 55 56 #endif // TOOLS_GN_STRING_UTILS_H_ 57