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.

83 lines
2.6 KiB

  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. //Fixes the build on old versions of ICU with Windows
  18. #include <stdio.h>
  19. #include "intl_convertcpp.h"
  20. #include <unicode/ustring.h>
  21. extern "C" {
  22. #include <php.h>
  23. }
  24. /* {{{ intl_stringFromChar */
  25. int intl_stringFromChar(UnicodeString &ret, char *str, int32_t str_len, UErrorCode *status)
  26. {
  27. //the number of UTF-16 code units is not larger than that of UTF-8 code
  28. //units, + 1 for the terminator
  29. int32_t capacity = str_len + 1;
  30. //no check necessary -- if NULL will fail ahead
  31. UChar *utf16 = ret.getBuffer(capacity);
  32. int32_t utf16_len = 0;
  33. *status = U_ZERO_ERROR;
  34. u_strFromUTF8WithSub(utf16, ret.getCapacity(), &utf16_len,
  35. str, str_len, U_SENTINEL /* no substitution */, NULL,
  36. status);
  37. ret.releaseBuffer(utf16_len);
  38. if (U_FAILURE(*status)) {
  39. ret.setToBogus();
  40. return FAILURE;
  41. }
  42. return SUCCESS;
  43. }
  44. /* }}} */
  45. /* {{{ intl_charFromString */
  46. int intl_charFromString(const UnicodeString &from, char **res, int *res_len, UErrorCode *status)
  47. {
  48. //the number of UTF-8 code units is not larger than that of UTF-16 code
  49. //units * 3 + 1 for the terminator
  50. int32_t capacity = from.length() * 3 + 1;
  51. if (from.isEmpty()) {
  52. *res = (char*)emalloc(1);
  53. **res = '\0';
  54. *res_len = 0;
  55. return SUCCESS;
  56. }
  57. *res = (char*)emalloc(capacity);
  58. *res_len = 0; //tbd
  59. const UChar *utf16buf = from.getBuffer();
  60. int32_t actual_len;
  61. u_strToUTF8WithSub(*res, capacity - 1, &actual_len, utf16buf, from.length(),
  62. U_SENTINEL, NULL, status);
  63. if (U_FAILURE(*status)) {
  64. efree(*res);
  65. *res = NULL;
  66. return FAILURE;
  67. }
  68. (*res)[actual_len] = '\0';
  69. *res_len = (int)actual_len;
  70. return SUCCESS;
  71. }
  72. /* }}} */