juce_CharacterFunctions.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. //==============================================================================
  22. #if JUCE_MSVC
  23. #pragma warning (push)
  24. #pragma warning (disable: 4514 4996)
  25. #endif
  26. juce_wchar CharacterFunctions::toUpperCase (const juce_wchar character) noexcept
  27. {
  28. return (juce_wchar) towupper ((wint_t) character);
  29. }
  30. juce_wchar CharacterFunctions::toLowerCase (const juce_wchar character) noexcept
  31. {
  32. return (juce_wchar) towlower ((wint_t) character);
  33. }
  34. bool CharacterFunctions::isUpperCase (const juce_wchar character) noexcept
  35. {
  36. #if JUCE_WINDOWS
  37. return iswupper ((wint_t) character) != 0;
  38. #else
  39. return toLowerCase (character) != character;
  40. #endif
  41. }
  42. bool CharacterFunctions::isLowerCase (const juce_wchar character) noexcept
  43. {
  44. #if JUCE_WINDOWS
  45. return iswlower ((wint_t) character) != 0;
  46. #else
  47. return toUpperCase (character) != character;
  48. #endif
  49. }
  50. #if JUCE_MSVC
  51. #pragma warning (pop)
  52. #endif
  53. //==============================================================================
  54. bool CharacterFunctions::isWhitespace (const char character) noexcept
  55. {
  56. return character == ' ' || (character <= 13 && character >= 9);
  57. }
  58. bool CharacterFunctions::isWhitespace (const juce_wchar character) noexcept
  59. {
  60. return iswspace ((wint_t) character) != 0;
  61. }
  62. bool CharacterFunctions::isDigit (const char character) noexcept
  63. {
  64. return (character >= '0' && character <= '9');
  65. }
  66. bool CharacterFunctions::isDigit (const juce_wchar character) noexcept
  67. {
  68. return iswdigit ((wint_t) character) != 0;
  69. }
  70. bool CharacterFunctions::isLetter (const char character) noexcept
  71. {
  72. return (character >= 'a' && character <= 'z')
  73. || (character >= 'A' && character <= 'Z');
  74. }
  75. bool CharacterFunctions::isLetter (const juce_wchar character) noexcept
  76. {
  77. return iswalpha ((wint_t) character) != 0;
  78. }
  79. bool CharacterFunctions::isLetterOrDigit (const char character) noexcept
  80. {
  81. return (character >= 'a' && character <= 'z')
  82. || (character >= 'A' && character <= 'Z')
  83. || (character >= '0' && character <= '9');
  84. }
  85. bool CharacterFunctions::isLetterOrDigit (const juce_wchar character) noexcept
  86. {
  87. return iswalnum ((wint_t) character) != 0;
  88. }
  89. bool CharacterFunctions::isPrintable (const char character) noexcept
  90. {
  91. return (character >= ' ' && character <= '~');
  92. }
  93. bool CharacterFunctions::isPrintable (const juce_wchar character) noexcept
  94. {
  95. return iswprint ((wint_t) character) != 0;
  96. }
  97. int CharacterFunctions::getHexDigitValue (const juce_wchar digit) noexcept
  98. {
  99. unsigned int d = (unsigned int) digit - '0';
  100. if (d < (unsigned int) 10)
  101. return (int) d;
  102. d += (unsigned int) ('0' - 'a');
  103. if (d < (unsigned int) 6)
  104. return (int) d + 10;
  105. d += (unsigned int) ('a' - 'A');
  106. if (d < (unsigned int) 6)
  107. return (int) d + 10;
  108. return -1;
  109. }
  110. double CharacterFunctions::mulexp10 (const double value, int exponent) noexcept
  111. {
  112. if (exponent == 0)
  113. return value;
  114. if (value == 0)
  115. return 0;
  116. const bool negative = (exponent < 0);
  117. if (negative)
  118. exponent = -exponent;
  119. double result = 1.0, power = 10.0;
  120. for (int bit = 1; exponent != 0; bit <<= 1)
  121. {
  122. if ((exponent & bit) != 0)
  123. {
  124. exponent ^= bit;
  125. result *= power;
  126. if (exponent == 0)
  127. break;
  128. }
  129. power *= power;
  130. }
  131. return negative ? (value / result) : (value * result);
  132. }
  133. juce_wchar CharacterFunctions::getUnicodeCharFromWindows1252Codepage (const uint8 c) noexcept
  134. {
  135. if (c < 0x80 || c >= 0xa0)
  136. return (juce_wchar) c;
  137. static const uint16 lookup[] = { 0x20AC, 0x0007, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
  138. 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0007, 0x017D, 0x0007,
  139. 0x0007, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
  140. 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x0007, 0x017E, 0x0178 };
  141. return (juce_wchar) lookup[c - 0x80];
  142. }