LCOV - code coverage report
Current view: top level - cpp/util - string_ref.cc (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 35 37 94.6 %
Date: 2015-12-10 22:15:08 Functions: 16 17 94.1 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc++/support/string_ref.h>
      35             : 
      36             : #include <string.h>
      37             : 
      38             : #include <algorithm>
      39             : #include <iostream>
      40             : 
      41             : namespace grpc {
      42             : 
      43             : const size_t string_ref::npos = size_t(-1);
      44             : 
      45           1 : string_ref& string_ref::operator=(const string_ref& rhs) {
      46           1 :   data_ = rhs.data_;
      47           1 :   length_ = rhs.length_;
      48           1 :   return *this;
      49             : }
      50             : 
      51     1646613 : string_ref::string_ref(const char* s) : data_(s), length_(strlen(s)) {}
      52             : 
      53           2 : string_ref string_ref::substr(size_t pos, size_t n) const {
      54           2 :   if (pos > length_) pos = length_;
      55           2 :   if (n > (length_ - pos)) n = length_ - pos;
      56           2 :   return string_ref(data_ + pos, n);
      57             : }
      58             : 
      59         259 : int string_ref::compare(string_ref x) const {
      60         259 :   size_t min_size = length_ < x.length_ ? length_ : x.length_;
      61         259 :   int r = memcmp(data_, x.data_, min_size);
      62         259 :   if (r < 0) return -1;
      63         203 :   if (r > 0) return 1;
      64          90 :   if (length_ < x.length_) return -1;
      65          87 :   if (length_ > x.length_) return 1;
      66          86 :   return 0;
      67             : }
      68             : 
      69           5 : bool string_ref::starts_with(string_ref x) const {
      70           5 :   return length_ >= x.length_ && (memcmp(data_, x.data_, x.length_) == 0);
      71             : }
      72             : 
      73           5 : bool string_ref::ends_with(string_ref x) const {
      74           8 :   return length_ >= x.length_ &&
      75           8 :          (memcmp(data_ + (length_ - x.length_), x.data_, x.length_) == 0);
      76             : }
      77             : 
      78           8 : size_t string_ref::find(string_ref s) const {
      79           8 :   auto it = std::search(cbegin(), cend(), s.cbegin(), s.cend());
      80           8 :   return it == cend() ? npos : std::distance(cbegin(), it);
      81             : }
      82             : 
      83           2 : size_t string_ref::find(char c) const {
      84           2 :   auto it = std::find(cbegin(), cend(), c);
      85           2 :   return it == cend() ? npos : std::distance(cbegin(), it);
      86             : }
      87             : 
      88          17 : bool operator==(string_ref x, string_ref y) { return x.compare(y) == 0; }
      89             : 
      90           3 : bool operator!=(string_ref x, string_ref y) { return x.compare(y) != 0; }
      91             : 
      92         229 : bool operator<(string_ref x, string_ref y) { return x.compare(y) < 0; }
      93             : 
      94           3 : bool operator<=(string_ref x, string_ref y) { return x.compare(y) <= 0; }
      95             : 
      96           1 : bool operator>(string_ref x, string_ref y) { return x.compare(y) > 0; }
      97             : 
      98           3 : bool operator>=(string_ref x, string_ref y) { return x.compare(y) >= 0; }
      99             : 
     100           0 : std::ostream& operator<<(std::ostream& out, const string_ref& string) {
     101           0 :   return out << grpc::string(string.begin(), string.end());
     102             : }
     103             : 
     104          75 : }  // namespace grpc

Generated by: LCOV version 1.11