Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

OPTIMIZED ENTERPRISE ANONIX

Name: Anonymous 2011-07-07 0:31

#ifndef OMG_OPTIMIZED_CTYPE_H_
#define OMG_OPTIMIZED_CTYPE_H_

/**
 *                        __ _   _  ____  _   _ _ _  _
 *                       /  |  \| |/ __ \|  \| | | \/ /
 *                      /   |   \ | |  | |   \ | |\  /
 *                     /  . | |\  | |__| | |\  | |/  \
 *                    /__/|_|_| \_|\____/|_| \_|_|_/\_\
 *       
 *                       OPTIMIZED ANONIX INTERFACES
 *
 *  -------------------------------------------------------------------------
 *
 *  Optimized branchless ctype facilities using bitwise operations. Assumes a
 *  fixed ASCII (ANSI_X3.110-1983) code page. Functions are ISO/IEC 9899:1999
 *  (C99) compliant. Requires an ISO/IEC 9899:1999 (C99) compliant compiler.
 * 
 *  Version: 1.666alpha
 *  Bug Reports: strawberryshake [at] 1337 [dot] jp
 */
 
inline int isblank(int c) { return (c == ' ') | (c == '\t') | (c == '\v'); }
inline int iscntrl(int c) { return (c <= 0x1F) | (c == 0x7F); }
inline int isdigit(int c) { return (((unsigned int)c) - ((unsigned int)'0')) <= ((unsigned int)('9' - '0')); }
inline int isgraph(int c) { return (((unsigned int)c) - ((unsigned int)'!')) <= ((unsigned int)('~' - '!')); }
inline int islower(int c) { return (((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('z' - 'a')); }
inline int isprint(int c) { return (((unsigned int)c) - ((unsigned int)' ')) <= ((unsigned int)('~' - ' ')); }
inline int isupper(int c) { return (((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('Z' - 'A')); }
inline int isalpha(int c) { return ::hrc::islower(c) | ::hrc::isupper(c); }
inline int isalnum(int c) { return ::hrc::islower(c) | ::hrc::isupper(c) | ::hrc::isdigit(c); }
inline int ispunct(int c) { return ~(::hrc::islower(c) | ::hrc::isupper(c) | ::hrc::isdigit(c)); }
inline int isspace(int c) { return ::hrc::isblank(c) | (c == '\f') | (c == '\n') | (c == '\r'); }

inline int isxdigit(int c) {
    return ::hrc::isdigit(c) |
        ((((unsigned int)c) - ((unsigned int)'a')) <= ((unsigned int)('f' - 'a'))) |
        ((((unsigned int)c) - ((unsigned int)'A')) <= ((unsigned int)('F' - 'A')));
}

inline int tolower(int c) {
    unsigned int upper = (unsigned int)::hrc::isupper(c);
    return (int)((((~upper) + 1) & ((unsigned int)(c - 'A' + 'a'))) | ((~(upper - 1)) & ((unsigned int)c)));
}

inline int toupper(int c) {
    unsigned int lower = (unsigned int)::hrc::islower(c);
    return (int)((((~lower) + 1) & ((unsigned int)(c - 'a' + 'A'))) | ((~(lower - 1)) & ((unsigned int)c)));
}

#endif

Name: Anonymous 2011-07-07 1:33

>>6
I know C. You apparently do not. You need to use int so that you can represent all C language characters plus EOF, which typically has a value of -1. Since in C89/C99, char could be less than 8-bits in size, there was no reliable way to guarantee being able to store EOF without first expanding to a larger type. In the new C1x and C++0x standards, char must be at least 8-bits in size, capable of representing all possible UTF-8 octets, meaning you really do need to use int.

Furthermore, when operating on such variables, it's usually best to work with variables that are the same size as the target architecture's machine word. On x86, it's often faster to use 32-bit or 64-bit integers and 8-bit integers in ALU operations.

That's why the functions in ctype.h in the ISO/IEC C standards specifications use int as well. In fact, C99 added stdint.h with types like int_fast16_t, int_fast32_t and int_fast64_t, and so on.

However, I have a feeling IHBT by you.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List