ImmutableList.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "Option.h"
  7. #include "ImmutableList.h"
  8. // Include it just for VC- non-VC++ platforms get the required definitions
  9. // from CommonPal.h
  10. #ifdef _MSC_VER
  11. #include <strsafe.h>
  12. #endif
  13. template<int chunkSize>
  14. void regex::ImmutableStringBuilder<chunkSize>::AppendInt32(int32 value)
  15. {
  16. WCHAR buffer[11]; // -2,147,483,648 w.o ',' + \0
  17. HRESULT hr = S_OK;
  18. hr = StringCchPrintfW(buffer, _countof(buffer), _u("%d"), value);
  19. AssertMsg(SUCCEEDED(hr), "StringCchPrintfW");
  20. if (FAILED(hr) )
  21. {
  22. Js::Throw::OutOfMemory();
  23. }
  24. // append to the stringBuilder string
  25. this->AppendWithCopy(buffer);
  26. }
  27. template<int chunkSize>
  28. void regex::ImmutableStringBuilder<chunkSize>::AppendUInt64(uint64 value)
  29. {
  30. WCHAR buffer[21]; // 18,446,744,073,709,551,615 w.o ',' + \0
  31. HRESULT hr = S_OK;
  32. hr = StringCchPrintfW(buffer, _countof(buffer), _u("%llu"), value);
  33. AssertMsg(SUCCEEDED(hr), "StringCchPrintfW");
  34. if (FAILED(hr) )
  35. {
  36. Js::Throw::OutOfMemory();
  37. }
  38. // append to the stringBuilder string
  39. this->AppendWithCopy(buffer);
  40. }
  41. template<int chunkSize>
  42. void regex::ImmutableStringBuilder<chunkSize>::AppendWithCopy(_In_z_ LPCWSTR str)
  43. {
  44. AssertMsg(str != nullptr, "str != nullptr");
  45. size_t strLength = wcslen(str) + 1; // include null-terminated
  46. WCHAR* buffer = HeapNewNoThrowArray(WCHAR, strLength);
  47. IfNullThrowOutOfMemory(buffer);
  48. wcsncpy_s(buffer, strLength, str, strLength);
  49. // append in front of the tracked allocated strings
  50. AllocatedStringChunk* newAllocatedString = HeapNewNoThrow(AllocatedStringChunk);
  51. if (newAllocatedString == nullptr)
  52. {
  53. // cleanup
  54. HeapDeleteArray(strLength, buffer);
  55. Js::Throw::OutOfMemory();
  56. }
  57. newAllocatedString->dataPtr = buffer;
  58. newAllocatedString->next = this->allocatedStringChunksHead;
  59. this->allocatedStringChunksHead = newAllocatedString;
  60. // append to the stringBuilder string
  61. this->Append(buffer);
  62. }
  63. // template instantiation
  64. template void regex::ImmutableStringBuilder<8>::AppendInt32(int32 value);
  65. template void regex::ImmutableStringBuilder<8>::AppendUInt64(uint64 value);