| 1092 | } |
| 1093 | |
| 1094 | template<class T> String BasicStringView<T>::join(const ArrayView<const StringView> strings) const { |
| 1095 | // Calculate size of the resulting string including delimiters |
| 1096 | const std::size_t delimiterSize = size(); |
| 1097 | std::size_t totalSize = strings.empty() ? 0 : (strings.size() - 1) * delimiterSize; |
| 1098 | for (const StringView s : strings) totalSize += s.size(); |
| 1099 | |
| 1100 | // Reserve memory for the resulting string |
| 1101 | String result{NoInit, totalSize}; |
| 1102 | |
| 1103 | // Join strings |
| 1104 | char* out = result.data(); |
| 1105 | char* const end = out + totalSize; |
| 1106 | for (const StringView string : strings) { |
| 1107 | const std::size_t stringSize = string.size(); |
| 1108 | // Apparently memcpy() can't be called with null pointers, even if size is zero. I call that bullying. |
| 1109 | if (stringSize != 0) { |
| 1110 | std::memcpy(out, string._data, stringSize); |
| 1111 | out += stringSize; |
| 1112 | } |
| 1113 | if (delimiterSize != 0 && out != end) { |
| 1114 | std::memcpy(out, _data, delimiterSize); |
| 1115 | out += delimiterSize; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | return result; |
| 1120 | } |
| 1121 | |
| 1122 | template<class T> String BasicStringView<T>::join(const std::initializer_list<StringView> strings) const { |
| 1123 | return join(arrayView(strings)); |