intl/icu/source/common/stringpiece.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (C) 2009-2013, International Business Machines
     2 // Corporation and others. All Rights Reserved.
     3 //
     4 // Copyright 2004 and onwards Google Inc.
     5 //
     6 // Author: wilsonh@google.com (Wilson Hsieh)
     7 //
     9 #include "unicode/utypes.h"
    10 #include "unicode/stringpiece.h"
    11 #include "cstring.h"
    12 #include "cmemory.h"
    14 U_NAMESPACE_BEGIN
    16 StringPiece::StringPiece(const char* str)
    17     : ptr_(str), length_((str == NULL) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { }
    19 StringPiece::StringPiece(const StringPiece& x, int32_t pos) {
    20   if (pos < 0) {
    21     pos = 0;
    22   } else if (pos > x.length_) {
    23     pos = x.length_;
    24   }
    25   ptr_ = x.ptr_ + pos;
    26   length_ = x.length_ - pos;
    27 }
    29 StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) {
    30   if (pos < 0) {
    31     pos = 0;
    32   } else if (pos > x.length_) {
    33     pos = x.length_;
    34   }
    35   if (len < 0) {
    36     len = 0;
    37   } else if (len > x.length_ - pos) {
    38     len = x.length_ - pos;
    39   }
    40   ptr_ = x.ptr_ + pos;
    41   length_ = len;
    42 }
    44 void StringPiece::set(const char* str) {
    45   ptr_ = str;
    46   if (str != NULL)
    47     length_ = static_cast<int32_t>(uprv_strlen(str));
    48   else
    49     length_ = 0;
    50 }
    52 U_EXPORT UBool U_EXPORT2
    53 operator==(const StringPiece& x, const StringPiece& y) {
    54   int32_t len = x.size();
    55   if (len != y.size()) {
    56     return false;
    57   }
    58   if (len == 0) {
    59     return true;
    60   }
    61   const char* p = x.data();
    62   const char* p2 = y.data();
    63   // Test last byte in case strings share large common prefix
    64   --len;
    65   if (p[len] != p2[len]) return false;
    66   // At this point we can, but don't have to, ignore the last byte.
    67   return uprv_memcmp(p, p2, len) == 0;
    68 }
    71 const int32_t StringPiece::npos = 0x7fffffff;
    73 U_NAMESPACE_END

mercurial