JSStringJoiner.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "JSStringJoiner.h"
  27. #include "ExceptionHelpers.h"
  28. #include "JSScope.h"
  29. #include "JSString.h"
  30. #include "Operations.h"
  31. #include <wtf/text/StringImpl.h>
  32. namespace JSC {
  33. // The destination is 16bits, at least one string is 16 bits.
  34. static inline void appendStringToData(UChar*& data, const String& string)
  35. {
  36. if (string.isNull())
  37. return;
  38. unsigned length = string.length();
  39. const StringImpl* stringImpl = string.impl();
  40. if (stringImpl->is8Bit()) {
  41. for (unsigned i = 0; i < length; ++i) {
  42. *data = stringImpl->characters8()[i];
  43. ++data;
  44. }
  45. } else {
  46. for (unsigned i = 0; i < length; ++i) {
  47. *data = stringImpl->characters16()[i];
  48. ++data;
  49. }
  50. }
  51. }
  52. // If the destination is 8bits, we know every string has to be 8bit.
  53. static inline void appendStringToData(LChar*& data, const String& string)
  54. {
  55. if (string.isNull())
  56. return;
  57. ASSERT(string.is8Bit());
  58. unsigned length = string.length();
  59. const StringImpl* stringImpl = string.impl();
  60. for (unsigned i = 0; i < length; ++i) {
  61. *data = stringImpl->characters8()[i];
  62. ++data;
  63. }
  64. }
  65. template<typename CharacterType>
  66. static inline PassRefPtr<StringImpl> joinStrings(const Vector<String>& strings, const String& separator, unsigned outputLength)
  67. {
  68. ASSERT(outputLength);
  69. CharacterType* data;
  70. RefPtr<StringImpl> outputStringImpl = StringImpl::tryCreateUninitialized(outputLength, data);
  71. if (!outputStringImpl)
  72. return PassRefPtr<StringImpl>();
  73. const String firstString = strings.first();
  74. appendStringToData(data, firstString);
  75. for (size_t i = 1; i < strings.size(); ++i) {
  76. appendStringToData(data, separator);
  77. appendStringToData(data, strings[i]);
  78. }
  79. ASSERT(data == (outputStringImpl->getCharacters<CharacterType>() + outputStringImpl->length()));
  80. return outputStringImpl.release();
  81. }
  82. JSValue JSStringJoiner::join(ExecState* exec)
  83. {
  84. if (!m_isValid)
  85. return throwOutOfMemoryError(exec);
  86. if (!m_strings.size())
  87. return jsEmptyString(exec);
  88. Checked<unsigned, RecordOverflow> separatorLength = m_separator.length();
  89. // FIXME: add special cases of joinStrings() for (separatorLength == 0) and (separatorLength == 1).
  90. ASSERT(m_strings.size() > 0);
  91. Checked<unsigned, RecordOverflow> totalSeparactorsLength = separatorLength * (m_strings.size() - 1);
  92. Checked<unsigned, RecordOverflow> outputStringSize = totalSeparactorsLength + m_accumulatedStringsLength;
  93. unsigned finalSize;
  94. if (outputStringSize.safeGet(finalSize) == CheckedState::DidOverflow)
  95. return throwOutOfMemoryError(exec);
  96. if (!outputStringSize)
  97. return jsEmptyString(exec);
  98. RefPtr<StringImpl> outputStringImpl;
  99. if (m_is8Bits)
  100. outputStringImpl = joinStrings<LChar>(m_strings, m_separator, finalSize);
  101. else
  102. outputStringImpl = joinStrings<UChar>(m_strings, m_separator, finalSize);
  103. if (!outputStringImpl)
  104. return throwOutOfMemoryError(exec);
  105. return JSString::create(exec->vm(), outputStringImpl.release());
  106. }
  107. }