unicode.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #pragma clang diagnostic push
  24. #pragma clang diagnostic ignored "-Wdeprecated"
  25. #pragma clang diagnostic ignored "-Wweak-vtables"
  26. #pragma clang diagnostic ignored "-Wsign-conversion"
  27. #pragma clang diagnostic ignored "-Wsuggest-override"
  28. #include <utf8.h>
  29. #pragma clang diagnostic pop
  30. #include <xlnt/utils/exceptions.hpp>
  31. #include <detail/unicode.hpp>
  32. namespace xlnt {
  33. namespace detail {
  34. std::u16string utf8_to_utf16(const std::string &utf8_string)
  35. {
  36. std::u16string result;
  37. utf8::utf8to16(utf8_string.begin(), utf8_string.end(), std::back_inserter(result));
  38. return result;
  39. }
  40. std::string utf16_to_utf8(const std::u16string &utf16_string)
  41. {
  42. std::string result;
  43. utf8::utf16to8(utf16_string.begin(), utf16_string.end(), std::back_inserter(result));
  44. return result;
  45. }
  46. std::string latin1_to_utf8(const std::string &latin1)
  47. {
  48. std::string utf8;
  49. for (auto character : latin1)
  50. {
  51. if (character >= 0)
  52. {
  53. utf8.push_back(character);
  54. }
  55. else
  56. {
  57. utf8.push_back(static_cast<char>(0xc0 + (character >> 6)));
  58. utf8.push_back(static_cast<char>(0x80 + (character & 0x3f)));
  59. }
  60. }
  61. return utf8;
  62. }
  63. size_t string_length(const std::string &utf8_string)
  64. {
  65. auto end_it = utf8::find_invalid(utf8_string.begin(), utf8_string.end());
  66. if (end_it != utf8_string.end())
  67. {
  68. throw xlnt::exception("Invalid UTF-8 encoding detected");
  69. }
  70. return static_cast<std::size_t>(utf8::distance(utf8_string.begin(), end_it));
  71. }
  72. } // namespace detail
  73. } // namespace xlnt