Changeset 103144 in webkit


Ignore:
Timestamp:
Dec 16, 2011 10:35:29 PM (12 years ago)
Author:
benjamin@webkit.org
Message:

Remove the duplicated code from ASCIICType.h
https://bugs.webkit.org/show_bug.cgi?id=74771

Patch by Benjamin Poulain <bpoulain@apple.com> on 2011-12-16
Reviewed by Andreas Kling.

The functions were sharing similar code and were defined for the various input types.
Use templates instead to avoid code duplication.

  • wtf/ASCIICType.h:

(WTF::isASCII):
(WTF::isASCIIAlpha):
(WTF::isASCIIAlphanumeric):
(WTF::isASCIIDigit):
(WTF::isASCIIHexDigit):
(WTF::isASCIILower):
(WTF::isASCIIOctalDigit):
(WTF::isASCIIPrintable):
(WTF::isASCIISpace):
(WTF::isASCIIUpper):
(WTF::toASCIILower):
(WTF::toASCIIUpper):
(WTF::toASCIIHexValue):
(WTF::lowerNibbleToASCIIHexDigit):
(WTF::upperNibbleToASCIIHexDigit):

Location:
trunk/Source/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r103142 r103144  
     12011-12-16  Benjamin Poulain  <bpoulain@apple.com>
     2
     3        Remove the duplicated code from ASCIICType.h
     4        https://bugs.webkit.org/show_bug.cgi?id=74771
     5
     6        Reviewed by Andreas Kling.
     7
     8        The functions were sharing similar code and were defined for the various input types.
     9        Use templates instead to avoid code duplication.
     10
     11        * wtf/ASCIICType.h:
     12        (WTF::isASCII):
     13        (WTF::isASCIIAlpha):
     14        (WTF::isASCIIAlphanumeric):
     15        (WTF::isASCIIDigit):
     16        (WTF::isASCIIHexDigit):
     17        (WTF::isASCIILower):
     18        (WTF::isASCIIOctalDigit):
     19        (WTF::isASCIIPrintable):
     20        (WTF::isASCIISpace):
     21        (WTF::isASCIIUpper):
     22        (WTF::toASCIILower):
     23        (WTF::toASCIIUpper):
     24        (WTF::toASCIIHexValue):
     25        (WTF::lowerNibbleToASCIIHexDigit):
     26        (WTF::upperNibbleToASCIIHexDigit):
     27
    1282011-12-16  Gavin Barraclough  <barraclough@apple.com>
    229
  • trunk/Source/JavaScriptCore/wtf/ASCIICType.h

    r85909 r103144  
    11/*
    2  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
     2 * Copyright (C) 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    4444namespace WTF {
    4545
    46     inline bool isASCII(char c) { return !(c & ~0x7F); }
    47     inline bool isASCII(unsigned short c) { return !(c & ~0x7F); }
    48 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    49     inline bool isASCII(wchar_t c) { return !(c & ~0x7F); }
    50 #endif
    51     inline bool isASCII(int c) { return !(c & ~0x7F); }
    52     inline bool isASCII(unsigned c) { return !(c & ~0x7F); }
     46template<typename CharType> inline bool isASCII(CharType c)
     47{
     48    return !(c & ~0x7F);
     49}
    5350
    54     inline bool isASCIIAlpha(char c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
    55     inline bool isASCIIAlpha(unsigned short c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
    56 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    57     inline bool isASCIIAlpha(wchar_t c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
    58 #endif
    59     inline bool isASCIIAlpha(int c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
    60     inline bool isASCIIAlpha(unsigned c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
     51template<typename CharType> inline bool isASCIIAlpha(CharType c)
     52{
     53    return (c | 0x20) >= 'a' && (c | 0x20) <= 'z';
     54}
    6155
    62     inline bool isASCIIAlphanumeric(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
    63     inline bool isASCIIAlphanumeric(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
    64 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    65     inline bool isASCIIAlphanumeric(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
    66 #endif
    67     inline bool isASCIIAlphanumeric(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
    68     inline bool isASCIIAlphanumeric(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
     56template<typename CharType> inline bool isASCIIAlphanumeric(CharType c)
     57{
     58    return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z');
     59}
    6960
    70     inline bool isASCIIDigit(char c) { return (c >= '0') & (c <= '9'); }
    71     inline bool isASCIIDigit(unsigned short c) { return (c >= '0') & (c <= '9'); }
    72 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    73     inline bool isASCIIDigit(wchar_t c) { return (c >= '0') & (c <= '9'); }
    74 #endif
    75     inline bool isASCIIDigit(int c) { return (c >= '0') & (c <= '9'); }
    76     inline bool isASCIIDigit(unsigned c) { return (c >= '0') & (c <= '9'); }
     61template<typename CharType> inline bool isASCIIDigit(CharType c)
     62{
     63    return (c >= '0') & (c <= '9');
     64}
    7765
    78     inline bool isASCIIHexDigit(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
    79     inline bool isASCIIHexDigit(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
    80 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    81     inline bool isASCIIHexDigit(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
    82 #endif
    83     inline bool isASCIIHexDigit(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
    84     inline bool isASCIIHexDigit(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
     66template<typename CharType> inline bool isASCIIHexDigit(CharType c)
     67{
     68    return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f');
     69}
    8570
    86     inline bool isASCIIOctalDigit(char c) { return (c >= '0') & (c <= '7'); }
    87     inline bool isASCIIOctalDigit(unsigned short c) { return (c >= '0') & (c <= '7'); }
    88 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    89     inline bool isASCIIOctalDigit(wchar_t c) { return (c >= '0') & (c <= '7'); }
    90 #endif
    91     inline bool isASCIIOctalDigit(int c) { return (c >= '0') & (c <= '7'); }
    92     inline bool isASCIIOctalDigit(unsigned c) { return (c >= '0') & (c <= '7'); }
     71template<typename CharType> inline bool isASCIILower(CharType c)
     72{
     73    return c >= 'a' && c <= 'z';
     74}
    9375
    94     inline bool isASCIILower(char c) { return c >= 'a' && c <= 'z'; }
    95     inline bool isASCIILower(unsigned short c) { return c >= 'a' && c <= 'z'; }
    96 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    97     inline bool isASCIILower(wchar_t c) { return c >= 'a' && c <= 'z'; }
    98 #endif
    99     inline bool isASCIILower(int c) { return c >= 'a' && c <= 'z'; }
    100     inline bool isASCIILower(unsigned c) { return c >= 'a' && c <= 'z'; }
     76template<typename CharType> inline bool isASCIIOctalDigit(CharType c)
     77{
     78    return (c >= '0') & (c <= '7');
     79}
    10180
    102     inline bool isASCIIUpper(char c) { return c >= 'A' && c <= 'Z'; }
    103     inline bool isASCIIUpper(unsigned short c) { return c >= 'A' && c <= 'Z'; }
    104 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    105     inline bool isASCIIUpper(wchar_t c) { return c >= 'A' && c <= 'Z'; }
    106 #endif
    107     inline bool isASCIIUpper(int c) { return c >= 'A' && c <= 'Z'; }
    108     inline bool isASCIIUpper(unsigned c) { return c >= 'A' && c <= 'Z'; }
     81template<typename CharType> inline bool isASCIIPrintable(CharType c)
     82{
     83    return c >= ' ' && c <= '~';
     84}
    10985
    110     /*
    111         Statistics from a run of Apple's page load test for callers of isASCIISpace:
     86/*
     87 Statistics from a run of Apple's page load test for callers of isASCIISpace:
    11288
    113             character          count
    114             ---------          -----
    115             non-spaces         689383
    116         20  space              294720
    117         0A  \n                 89059
    118         09  \t                 28320
    119         0D  \r                 0
    120         0C  \f                 0
    121         0B  \v                 0
    122     */
    123     inline bool isASCIISpace(char c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
    124     inline bool isASCIISpace(unsigned short c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
    125 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    126     inline bool isASCIISpace(wchar_t c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
    127 #endif
    128     inline bool isASCIISpace(int c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
    129     inline bool isASCIISpace(unsigned c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
     89 character          count
     90 ---------          -----
     91 non-spaces         689383
     92 20  space          294720
     93 0A  \n             89059
     94 09  \t             28320
     95 0D  \r             0
     96 0C  \f             0
     97 0B  \v             0
     98 */
     99template<typename CharType> inline bool isASCIISpace(CharType c)
     100{
     101    return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9));
     102}
    130103
    131     inline char toASCIILower(char c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
    132     inline unsigned short toASCIILower(unsigned short c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
    133 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    134     inline wchar_t toASCIILower(wchar_t c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
    135 #endif
    136     inline int toASCIILower(int c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
    137     inline unsigned toASCIILower(unsigned c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
     104template<typename CharType> inline bool isASCIIUpper(CharType c)
     105{
     106    return c >= 'A' && c <= 'Z';
     107}
    138108
    139     // FIXME: Why do these need static_cast?
    140     inline char toASCIIUpper(char c) { return static_cast<char>(c & ~((c >= 'a' && c <= 'z') << 5)); }
    141     inline unsigned short toASCIIUpper(unsigned short c) { return static_cast<unsigned short>(c & ~((c >= 'a' && c <= 'z') << 5)); }
    142 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    143     inline wchar_t toASCIIUpper(wchar_t c) { return static_cast<wchar_t>(c & ~((c >= 'a' && c <= 'z') << 5)); }
    144 #endif
    145     inline int toASCIIUpper(int c) { return static_cast<int>(c & ~((c >= 'a' && c <= 'z') << 5)); }
    146     inline unsigned toASCIIUpper(unsigned c) { return static_cast<unsigned>(c & ~((c >= 'a' && c <= 'z') << 5)); }
     109template<typename CharType> inline CharType toASCIILower(CharType c)
     110{
     111    return c | ((c >= 'A' && c <= 'Z') << 5);
     112}
    147113
    148     inline int toASCIIHexValue(char c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
    149     inline int toASCIIHexValue(char upperValue, char lowerValue) { ASSERT(isASCIIHexDigit(upperValue) && isASCIIHexDigit(lowerValue)); return ((toASCIIHexValue(upperValue) << 4) & 0xF0) | toASCIIHexValue(lowerValue); }
    150     inline int toASCIIHexValue(unsigned short c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
    151 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    152     inline int toASCIIHexValue(wchar_t c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
    153 #endif
    154     inline int toASCIIHexValue(int c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
    155     inline int toASCIIHexValue(unsigned c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
     114template<typename CharType> inline CharType toASCIIUpper(CharType c)
     115{
     116    return c & ~((c >= 'a' && c <= 'z') << 5);
     117}
    156118
    157     inline char lowerNibbleToASCIIHexDigit(char c) { char nibble = c & 0xF; return nibble < 10 ? '0' + nibble : 'A' + nibble - 10; }
    158     inline char upperNibbleToASCIIHexDigit(char c) { char nibble = (c >> 4) & 0xF; return nibble < 10 ? '0' + nibble : 'A' + nibble - 10; }
     119template<typename CharType> inline int toASCIIHexValue(CharType c)
     120{
     121    ASSERT(isASCIIHexDigit(c));
     122    return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF;
     123}
    159124
    160     inline bool isASCIIPrintable(char c) { return c >= ' ' && c <= '~'; }
    161     inline bool isASCIIPrintable(unsigned short c) { return c >= ' ' && c <= '~'; }
    162 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
    163     inline bool isASCIIPrintable(wchar_t c) { return c >= ' ' && c <= '~'; }
    164 #endif
    165     inline bool isASCIIPrintable(int c) { return c >= ' ' && c <= '~'; }
    166     inline bool isASCIIPrintable(unsigned c) { return c >= ' ' && c <= '~'; }
     125template<typename CharType> inline int toASCIIHexValue(CharType upperValue, CharType lowerValue)
     126{
     127    ASSERT(isASCIIHexDigit(upperValue) && isASCIIHexDigit(lowerValue));
     128    return ((toASCIIHexValue(upperValue) << 4) & 0xF0) | toASCIIHexValue(lowerValue);
     129}
     130
     131inline char lowerNibbleToASCIIHexDigit(char c)
     132{
     133    char nibble = c & 0xF;
     134    return nibble < 10 ? '0' + nibble : 'A' + nibble - 10;
     135}
     136
     137inline char upperNibbleToASCIIHexDigit(char c)
     138{
     139    char nibble = (c >> 4) & 0xF;
     140    return nibble < 10 ? '0' + nibble : 'A' + nibble - 10;
     141}
     142
    167143}
    168144
Note: See TracChangeset for help on using the changeset viewer.