nxmutablestring.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "nxmutablestring.h"
  2. #include "foundation/error.h"
  3. /* currently this is closely coupled with the nx_string_t implementation. beware! */
  4. extern HANDLE string_heap;
  5. void NXMutableStringDestroy(nx_mutable_string_t mutable_string)
  6. {
  7. if (mutable_string)
  8. {
  9. if (mutable_string->nx_string_data)
  10. NXStringRelease(mutable_string->nx_string_data);
  11. HeapFree(string_heap, 0, mutable_string);
  12. }
  13. }
  14. nx_mutable_string_t NXMutableStringCreateFromXML(const nsxml_char_t *characters, size_t num_characters)
  15. {
  16. nx_mutable_string_t mutable_string = (nx_mutable_string_t)HeapAlloc(string_heap, 0, sizeof(nx_mutable_string_struct_t));
  17. NXStringCreateWithBytes(&mutable_string->nx_string_data, characters, num_characters*2, nx_charset_utf16le);
  18. mutable_string->allocation_length = num_characters;
  19. return mutable_string;
  20. }
  21. int NXMutableStringGrowFromXML(nx_mutable_string_t mutable_string, const nsxml_char_t *characters, size_t num_characters)
  22. {
  23. if (mutable_string->nx_string_data->len + num_characters + 1 > mutable_string->allocation_length)
  24. {
  25. nx_string_t new_string = NXStringRealloc(mutable_string->nx_string_data, mutable_string->nx_string_data->len + num_characters + 1);
  26. if (!new_string)
  27. return NErr_OutOfMemory;
  28. mutable_string->nx_string_data = new_string;
  29. mutable_string->allocation_length = mutable_string->nx_string_data->len + num_characters + 1;
  30. }
  31. memcpy(mutable_string->nx_string_data->string + mutable_string->nx_string_data->len, characters, num_characters*sizeof(wchar_t));
  32. mutable_string->nx_string_data->len += num_characters;
  33. mutable_string->nx_string_data->string[mutable_string->nx_string_data->len]=0; /* well null terminate */
  34. return NErr_Success;
  35. }