Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.2KB

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