1695b41eeSopenharmony_ci// Copyright 2011 Google Inc. All Rights Reserved. 2695b41eeSopenharmony_ci// 3695b41eeSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4695b41eeSopenharmony_ci// you may not use this file except in compliance with the License. 5695b41eeSopenharmony_ci// You may obtain a copy of the License at 6695b41eeSopenharmony_ci// 7695b41eeSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8695b41eeSopenharmony_ci// 9695b41eeSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10695b41eeSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11695b41eeSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12695b41eeSopenharmony_ci// See the License for the specific language governing permissions and 13695b41eeSopenharmony_ci// limitations under the License. 14695b41eeSopenharmony_ci 15695b41eeSopenharmony_ci#ifndef NINJA_STRINGPIECE_H_ 16695b41eeSopenharmony_ci#define NINJA_STRINGPIECE_H_ 17695b41eeSopenharmony_ci 18695b41eeSopenharmony_ci#include <string> 19695b41eeSopenharmony_ci 20695b41eeSopenharmony_ci#include <string.h> 21695b41eeSopenharmony_ci 22695b41eeSopenharmony_ci/// StringPiece represents a slice of a string whose memory is managed 23695b41eeSopenharmony_ci/// externally. It is useful for reducing the number of std::strings 24695b41eeSopenharmony_ci/// we need to allocate. 25695b41eeSopenharmony_cistruct StringPiece { 26695b41eeSopenharmony_ci typedef const char* const_iterator; 27695b41eeSopenharmony_ci 28695b41eeSopenharmony_ci StringPiece() : str_(NULL), len_(0) {} 29695b41eeSopenharmony_ci 30695b41eeSopenharmony_ci /// The constructors intentionally allow for implicit conversions. 31695b41eeSopenharmony_ci StringPiece(const std::string& str) : str_(str.data()), len_(str.size()) {} 32695b41eeSopenharmony_ci StringPiece(const char* str) : str_(str), len_(strlen(str)) {} 33695b41eeSopenharmony_ci 34695b41eeSopenharmony_ci StringPiece(const char* str, size_t len) : str_(str), len_(len) {} 35695b41eeSopenharmony_ci 36695b41eeSopenharmony_ci bool operator==(const StringPiece& other) const { 37695b41eeSopenharmony_ci return len_ == other.len_ && memcmp(str_, other.str_, len_) == 0; 38695b41eeSopenharmony_ci } 39695b41eeSopenharmony_ci 40695b41eeSopenharmony_ci bool operator!=(const StringPiece& other) const { 41695b41eeSopenharmony_ci return !(*this == other); 42695b41eeSopenharmony_ci } 43695b41eeSopenharmony_ci 44695b41eeSopenharmony_ci /// Convert the slice into a full-fledged std::string, copying the 45695b41eeSopenharmony_ci /// data into a new string. 46695b41eeSopenharmony_ci std::string AsString() const { 47695b41eeSopenharmony_ci return len_ ? std::string(str_, len_) : std::string(); 48695b41eeSopenharmony_ci } 49695b41eeSopenharmony_ci 50695b41eeSopenharmony_ci const_iterator begin() const { 51695b41eeSopenharmony_ci return str_; 52695b41eeSopenharmony_ci } 53695b41eeSopenharmony_ci 54695b41eeSopenharmony_ci const_iterator end() const { 55695b41eeSopenharmony_ci return str_ + len_; 56695b41eeSopenharmony_ci } 57695b41eeSopenharmony_ci 58695b41eeSopenharmony_ci char operator[](size_t pos) const { 59695b41eeSopenharmony_ci return str_[pos]; 60695b41eeSopenharmony_ci } 61695b41eeSopenharmony_ci 62695b41eeSopenharmony_ci size_t size() const { 63695b41eeSopenharmony_ci return len_; 64695b41eeSopenharmony_ci } 65695b41eeSopenharmony_ci 66695b41eeSopenharmony_ci const char* str_; 67695b41eeSopenharmony_ci size_t len_; 68695b41eeSopenharmony_ci}; 69695b41eeSopenharmony_ci 70695b41eeSopenharmony_ci#endif // NINJA_STRINGPIECE_H_ 71