intl_convertcpp.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. +----------------------------------------------------------------------+
  3. | This source file is subject to version 3.01 of the PHP license, |
  4. | that is bundled with this package in the file LICENSE, and is |
  5. | available through the world-wide-web at the following url: |
  6. | https://www.php.net/license/3_01.txt |
  7. | If you did not receive a copy of the PHP license and are unable to |
  8. | obtain it through the world-wide-web, please send a note to |
  9. | license@php.net so we can mail you a copy immediately. |
  10. +----------------------------------------------------------------------+
  11. | Authors: Gustavo Lopes <cataphract@php.net> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #include "intl_cppshims.h"
  15. #include "intl_convertcpp.h"
  16. #include <unicode/ustring.h>
  17. extern "C" {
  18. #include <php.h>
  19. }
  20. /* {{{ intl_stringFromChar */
  21. int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCode *status)
  22. {
  23. if(str_len > INT32_MAX) {
  24. *status = U_BUFFER_OVERFLOW_ERROR;
  25. ret.setToBogus();
  26. return FAILURE;
  27. }
  28. //the number of UTF-16 code units is not larger than that of UTF-8 code
  29. //units, + 1 for the terminator
  30. int32_t capacity = (int32_t)str_len + 1;
  31. //no check necessary -- if NULL will fail ahead
  32. UChar *utf16 = ret.getBuffer(capacity);
  33. int32_t utf16_len = 0;
  34. *status = U_ZERO_ERROR;
  35. u_strFromUTF8WithSub(utf16, ret.getCapacity(), &utf16_len,
  36. str, str_len, U_SENTINEL /* no substitution */, NULL,
  37. status);
  38. ret.releaseBuffer(utf16_len);
  39. if (U_FAILURE(*status)) {
  40. ret.setToBogus();
  41. return FAILURE;
  42. }
  43. return SUCCESS;
  44. }
  45. /* }}} */
  46. /* {{{ intl_charFromString
  47. * faster than doing intl_convert_utf16_to_utf8(
  48. * from.getBuffer(), from.length(), &status),
  49. * but consumes more memory */
  50. zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status)
  51. {
  52. zend_string *u8res;
  53. if (from.isBogus()) {
  54. return NULL;
  55. }
  56. //the number of UTF-8 code units is not larger than that of UTF-16 code
  57. //units * 3
  58. int32_t capacity = from.length() * 3;
  59. if (from.isEmpty()) {
  60. return ZSTR_EMPTY_ALLOC();
  61. }
  62. u8res = zend_string_alloc(capacity, 0);
  63. const UChar *utf16buf = from.getBuffer();
  64. int32_t actual_len;
  65. u_strToUTF8WithSub(ZSTR_VAL(u8res), capacity, &actual_len, utf16buf, from.length(),
  66. U_SENTINEL, NULL, status);
  67. if (U_FAILURE(*status)) {
  68. zend_string_free(u8res);
  69. return NULL;
  70. }
  71. ZSTR_VAL(u8res)[actual_len] = '\0';
  72. ZSTR_LEN(u8res) = actual_len;
  73. return u8res;
  74. }
  75. /* }}} */