intl_convertcpp.cpp 2.7 KB

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