ascii.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Fast, compact and locale-independent tools for ASCII-characters processing
  2. //
  3. // Platform: ISO C++ 98/11
  4. // $Id$
  5. //
  6. // (c) __vic 2016
  7. #ifndef __VIC_ASCII_H
  8. #define __VIC_ASCII_H
  9. #include<__vic/defs.h>
  10. // FreeBSD 10 defines this
  11. #ifdef isascii
  12. #undef isascii
  13. #endif
  14. namespace __vic { namespace ascii {
  15. //----------------------------------------------------------------------------
  16. __VIC_CONSTEXPR_FUNC bool isdigit(char c)
  17. {
  18. return '0' <= c && c <= '9';
  19. }
  20. //----------------------------------------------------------------------------
  21. __VIC_CONSTEXPR_FUNC bool isxdigit(char c)
  22. {
  23. return isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
  24. }
  25. //----------------------------------------------------------------------------
  26. __VIC_CONSTEXPR_FUNC bool islower(char c)
  27. {
  28. return 'a' <= c && c <= 'z';
  29. }
  30. //----------------------------------------------------------------------------
  31. __VIC_CONSTEXPR_FUNC bool isupper(char c)
  32. {
  33. return 'A' <= c && c <= 'Z';
  34. }
  35. //----------------------------------------------------------------------------
  36. __VIC_CONSTEXPR_FUNC bool isalpha(char c)
  37. {
  38. return islower(c) || isupper(c);
  39. }
  40. //----------------------------------------------------------------------------
  41. __VIC_CONSTEXPR_FUNC bool isalnum(char c)
  42. {
  43. return isalpha(c) || isdigit(c);
  44. }
  45. //----------------------------------------------------------------------------
  46. __VIC_CONSTEXPR_FUNC bool isspace(char c)
  47. {
  48. return c == ' ' || ('\t' <= c && c <= '\r');
  49. }
  50. //----------------------------------------------------------------------------
  51. __VIC_CONSTEXPR_FUNC bool isascii(char c)
  52. {
  53. return static_cast<unsigned char>(c) < 0x7FU;
  54. }
  55. //----------------------------------------------------------------------------
  56. //----------------------------------------------------------------------------
  57. __VIC_CONSTEXPR_FUNC char todigit(int n)
  58. {
  59. // assert(0 <= n && n <= 9);
  60. return n + '0';
  61. }
  62. //----------------------------------------------------------------------------
  63. // Convert digit to integer number
  64. // Returns -1 if !isdigit(ch)
  65. __VIC_CONSTEXPR_FUNC int digit_to_number(char ch)
  66. {
  67. return '0' <= ch && ch <= '9' ? ch - '0' : -1;
  68. }
  69. //----------------------------------------------------------------------------
  70. __VIC_CONSTEXPR_FUNC char toxdigit_upper(int n)
  71. {
  72. // assert(0 <= n && n <= 15);
  73. return n < 10 ? n + '0' : n - 10 + 'A';
  74. }
  75. //----------------------------------------------------------------------------
  76. __VIC_CONSTEXPR_FUNC char toxdigit_lower(int n)
  77. {
  78. // assert(0 <= n && n <= 15);
  79. return n < 10 ? n + '0' : n - 10 + 'a';
  80. }
  81. //----------------------------------------------------------------------------
  82. // Convert HEX-digit to integer number
  83. // Returns -1 if !isxdigit(ch)
  84. __VIC_CONSTEXPR_FUNC int xdigit_to_number(char ch)
  85. {
  86. return '0' <= ch && ch <= '9' ? ch - '0' :
  87. 'A' <= ch && ch <= 'F' ? ch - 'A' + 10 :
  88. 'a' <= ch && ch <= 'f' ? ch - 'a' + 10 :
  89. -1; // Not a hex digit
  90. }
  91. //----------------------------------------------------------------------------
  92. //----------------------------------------------------------------------------
  93. // Precondition: isupper(upper)
  94. __VIC_CONSTEXPR_FUNC char upper_to_lower(char upper)
  95. {
  96. // Set the 5-th bit
  97. return upper | (1 << 5); // or: upper - ('a' - 'A')
  98. }
  99. //----------------------------------------------------------------------------
  100. // Precondition: islower(lower)
  101. __VIC_CONSTEXPR_FUNC char lower_to_upper(char lower)
  102. {
  103. // Clear the 5-th bit
  104. return lower & ~(1 << 5); // or: lower + ('a' - 'A')
  105. }
  106. //----------------------------------------------------------------------------
  107. __VIC_CONSTEXPR_FUNC char tolower(char c)
  108. {
  109. return isupper(c) ? upper_to_lower(c) : c;
  110. }
  111. //----------------------------------------------------------------------------
  112. __VIC_CONSTEXPR_FUNC char toupper(char c)
  113. {
  114. return islower(c) ? lower_to_upper(c) : c;
  115. }
  116. //----------------------------------------------------------------------------
  117. __VIC_CONSTEXPR_FUNC bool equal_icase(char ch1, char ch2)
  118. {
  119. return tolower(ch1) == tolower(ch2);
  120. }
  121. //----------------------------------------------------------------------------
  122. }} // namespace
  123. #endif // header guard