Changeset 143049 in webkit


Ignore:
Timestamp:
Feb 15, 2013 1:53:07 PM (11 years ago)
Author:
Christophe Dumez
Message:

Add CString operators for comparison with const char*
https://bugs.webkit.org/show_bug.cgi?id=109947

Reviewed by Darin Adler.

Source/WTF:

Add operators to WTF::CString for equality/inequality comparison
with const char* strings. This avoids constructing a CString
from a const char* in such cases, which is can be expensive as
it would copy it and call strlen().

  • wtf/text/CString.cpp:

(WTF::operator==): Use memcmp instead of strncmp to compare the
CString buffers as we know they are the same size and we don't
want to scan for terminating null byte.
(WTF):

  • wtf/text/CString.h:

(WTF):
(WTF::operator!=):

Tools:

Add tests for WTF::CString's comparison operators.

  • TestWebKitAPI/Tests/WTF/CString.cpp:

(TEST):

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r143027 r143049  
     12013-02-15  Christophe Dumez  <ch.dumez@sisa.samsung.com>
     2
     3        Add CString operators for comparison with const char*
     4        https://bugs.webkit.org/show_bug.cgi?id=109947
     5
     6        Reviewed by Darin Adler.
     7
     8        Add operators to WTF::CString for equality/inequality comparison
     9        with const char* strings. This avoids constructing a CString
     10        from a const char* in such cases, which is can be expensive as
     11        it would copy it and call strlen().
     12
     13        * wtf/text/CString.cpp:
     14        (WTF::operator==): Use memcmp instead of strncmp to compare the
     15        CString buffers as we know they are the same size and we don't
     16        want to scan for terminating null byte.
     17        (WTF):
     18        * wtf/text/CString.h:
     19        (WTF):
     20        (WTF::operator!=):
     21
    1222013-02-15  Geoffrey Garen  <ggaren@apple.com>
    223
  • trunk/Source/WTF/wtf/text/CString.cpp

    r142004 r143049  
    111111    if (a.length() != b.length())
    112112        return false;
    113     return !strncmp(a.data(), b.data(), min(a.length(), b.length()));
     113    return !memcmp(a.data(), b.data(), a.length());
     114}
     115
     116bool operator==(const CString& a, const char* b)
     117{
     118    if (a.isNull() != !b)
     119        return false;
     120    if (!b)
     121        return true;
     122    return !strcmp(a.data(), b);
    114123}
    115124
  • trunk/Source/WTF/wtf/text/CString.h

    r142004 r143049  
    8585WTF_EXPORT_PRIVATE bool operator==(const CString& a, const CString& b);
    8686inline bool operator!=(const CString& a, const CString& b) { return !(a == b); }
     87WTF_EXPORT_PRIVATE bool operator==(const CString& a, const char* b);
     88inline bool operator!=(const CString& a, const char* b) { return !(a == b); }
    8789
    8890} // namespace WTF
  • trunk/Tools/ChangeLog

    r143033 r143049  
     12013-02-15  Christophe Dumez  <ch.dumez@sisa.samsung.com>
     2
     3        Add CString operators for comparison with const char*
     4        https://bugs.webkit.org/show_bug.cgi?id=109947
     5
     6        Reviewed by Darin Adler.
     7
     8        Add tests for WTF::CString's comparison operators.
     9
     10        * TestWebKitAPI/Tests/WTF/CString.cpp:
     11        (TEST):
     12
    1132013-02-15  Zan Dobersek  <zdobersek@igalia.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/CString.cpp

    r126191 r143049  
    108108    ASSERT_STREQ(copy.data(), initialString);
    109109}
     110
     111TEST(WTF, CStringComparison)
     112{
     113    // Comparison with another CString.
     114    CString a;
     115    CString b;
     116    ASSERT_TRUE(a == b);
     117    ASSERT_FALSE(a != b);
     118    a = "a";
     119    b = CString();
     120    ASSERT_FALSE(a == b);
     121    ASSERT_TRUE(a != b);
     122    a = "a";
     123    b = "b";
     124    ASSERT_FALSE(a == b);
     125    ASSERT_TRUE(a != b);
     126    a = "a";
     127    b = "a";
     128    ASSERT_TRUE(a == b);
     129    ASSERT_FALSE(a != b);
     130    a = "a";
     131    b = "aa";
     132    ASSERT_FALSE(a == b);
     133    ASSERT_TRUE(a != b);
     134    a = "";
     135    b = "";
     136    ASSERT_TRUE(a == b);
     137    ASSERT_FALSE(a != b);
     138    a = "";
     139    b = CString();
     140    ASSERT_FALSE(a == b);
     141    ASSERT_TRUE(a != b);
     142    a = "a";
     143    b = "";
     144    ASSERT_FALSE(a == b);
     145    ASSERT_TRUE(a != b);
     146
     147    // Comparison with a const char*.
     148    CString c;
     149    const char* d = 0;
     150    ASSERT_TRUE(c == d);
     151    ASSERT_FALSE(c != d);
     152    c = "c";
     153    d = 0;
     154    ASSERT_FALSE(c == d);
     155    ASSERT_TRUE(c != d);
     156    c = CString();
     157    d = "d";
     158    ASSERT_FALSE(c == d);
     159    ASSERT_TRUE(c != d);
     160    c = "c";
     161    d = "d";
     162    ASSERT_FALSE(c == d);
     163    ASSERT_TRUE(c != d);
     164    c = "c";
     165    d = "c";
     166    ASSERT_TRUE(c == d);
     167    ASSERT_FALSE(c != d);
     168    c = "c";
     169    d = "cc";
     170    ASSERT_FALSE(c == d);
     171    ASSERT_TRUE(c != d);
     172    c = "cc";
     173    d = "c";
     174    ASSERT_FALSE(c == d);
     175    ASSERT_TRUE(c != d);
     176    c = "";
     177    d = "";
     178    ASSERT_TRUE(c == d);
     179    ASSERT_FALSE(c != d);
     180    c = "";
     181    d = 0;
     182    ASSERT_FALSE(c == d);
     183    ASSERT_TRUE(c != d);
     184    c = CString();
     185    d = "";
     186    ASSERT_FALSE(c == d);
     187    ASSERT_TRUE(c != d);
     188    c = "a";
     189    d = "";
     190    ASSERT_FALSE(c == d);
     191    ASSERT_TRUE(c != d);
     192    c = "";
     193    d = "b";
     194    ASSERT_FALSE(c == d);
     195    ASSERT_TRUE(c != d);
     196}
Note: See TracChangeset for help on using the changeset viewer.