ImmutableList.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonDataStructuresPch.h"
  6. #include <strsafe.h>
  7. #include "Option.h"
  8. #include "ImmutableList.h"
  9. template<int chunkSize>
  10. void regex::ImmutableStringBuilder<chunkSize>::AppendInt32(int32 value)
  11. {
  12. WCHAR buffer[11]; // -2,147,483,648 w.o ',' + \0
  13. HRESULT hr = S_OK;
  14. hr = StringCchPrintfW(buffer, _countof(buffer), _u("%d"), value);
  15. AssertMsg(SUCCEEDED(hr), "StringCchPrintfW");
  16. if (FAILED(hr) )
  17. {
  18. Js::Throw::OutOfMemory();
  19. }
  20. // append to the stringBuilder string
  21. this->AppendWithCopy(buffer);
  22. }
  23. template<int chunkSize>
  24. void regex::ImmutableStringBuilder<chunkSize>::AppendUInt64(uint64 value)
  25. {
  26. WCHAR buffer[21]; // 18,446,744,073,709,551,615 w.o ',' + \0
  27. HRESULT hr = S_OK;
  28. hr = StringCchPrintfW(buffer, _countof(buffer), _u("%llu"), value);
  29. AssertMsg(SUCCEEDED(hr), "StringCchPrintfW");
  30. if (FAILED(hr) )
  31. {
  32. Js::Throw::OutOfMemory();
  33. }
  34. // append to the stringBuilder string
  35. this->AppendWithCopy(buffer);
  36. }
  37. template<int chunkSize>
  38. void regex::ImmutableStringBuilder<chunkSize>::AppendWithCopy(_In_z_ LPCWSTR str)
  39. {
  40. AssertMsg(str != nullptr, "str != nullptr");
  41. size_t strLength = wcslen(str) + 1; // include null-terminated
  42. WCHAR* buffer = new WCHAR[strLength];
  43. IfNullThrowOutOfMemory(buffer);
  44. wcsncpy_s(buffer, strLength, str, strLength);
  45. // append in front of the tracked allocated strings
  46. AllocatedStringChunk* newAllocatedString = new AllocatedStringChunk();
  47. if (newAllocatedString == nullptr)
  48. {
  49. // cleanup
  50. delete[] buffer;
  51. Js::Throw::OutOfMemory();
  52. }
  53. newAllocatedString->dataPtr = buffer;
  54. newAllocatedString->next = this->allocatedStringChunksHead;
  55. this->allocatedStringChunksHead = newAllocatedString;
  56. // append to the stringBuilder string
  57. this->Append(buffer);
  58. }
  59. // template instantiation
  60. template void regex::ImmutableStringBuilder<8>::AppendInt32(int32 value);
  61. template void regex::ImmutableStringBuilder<8>::AppendUInt64(uint64 value);