From 327a8f36be328dab951cb6584fb22e55ae637aa0 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Fri, 9 Jun 2017 18:16:38 -0700 Subject: [PATCH] Minimize header file inclusion --- paddle/strings/stringpiece.cc | 8 +++++++- paddle/strings/stringpiece.h | 8 +------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/paddle/strings/stringpiece.cc b/paddle/strings/stringpiece.cc index 4f788c6ecd5..415b3558d5d 100644 --- a/paddle/strings/stringpiece.cc +++ b/paddle/strings/stringpiece.cc @@ -16,11 +16,11 @@ #include "paddle/strings/stringpiece.h" -// #include #include #include #include +#include namespace paddle { @@ -39,6 +39,12 @@ StringPiece::StringPiece(const char* s) : data_(s) { StringPiece::StringPiece(const std::string& s) : data_(s.data()), size_(s.size()) {} +char StringPiece::operator[](size_t n) const { + if (n >= len()) + throw std::invalid_argument("index out of StringPiece length"); + return data_[n]; +} + int Compare(StringPiece a, StringPiece b) { const size_t min_len = (a.len() < b.len()) ? a.len() : b.len(); int r = memcmp(a.data(), b.data(), min_len); diff --git a/paddle/strings/stringpiece.h b/paddle/strings/stringpiece.h index 413b65d3841..89aa084a292 100644 --- a/paddle/strings/stringpiece.h +++ b/paddle/strings/stringpiece.h @@ -16,9 +16,6 @@ #pragma once -#include - -#include #include namespace paddle { @@ -46,10 +43,7 @@ public: const char* data() const { return data_; } size_t len() const { return size_; } - char operator[](size_t n) const { - assert(n < len()); - return data_[n]; - } + char operator[](size_t n) const; // StringPiece doesn't own the string, so both iterator and const // iterator are const char* indeed. -- GitLab