| 164 | } |
| 165 | |
| 166 | static std::vector<std::string_view> javaStyleSplitRegex( |
| 167 | std::string_view input, |
| 168 | const icu::LocalPointer<icu::RegexPattern>& pattern, |
| 169 | int32_t limit) { |
| 170 | UErrorCode status = U_ZERO_ERROR; |
| 171 | icu::UnicodeString UInput = icu::UnicodeString::fromUTF8( |
| 172 | icu::StringPiece(input.data(), input.size())); |
| 173 | std::vector<std::string_view> results; |
| 174 | icu::LocalPointer<icu::RegexMatcher> matcher( |
| 175 | pattern->matcher(UInput, status)); |
| 176 | size_t index = 0; |
| 177 | bool matchLimited = limit > 0; |
| 178 | while (matcher->find()) { |
| 179 | size_t matchStart = matcher->start(status); |
| 180 | BOLT_CHECK( |
| 181 | U_SUCCESS(status), |
| 182 | "Error split by regex pattern: {}", |
| 183 | u_errorName(status)); |
| 184 | size_t matchEnd = matcher->end(status); |
| 185 | BOLT_CHECK( |
| 186 | U_SUCCESS(status), |
| 187 | "Error split by regex pattern: {}", |
| 188 | u_errorName(status)); |
| 189 | auto origIndex = stringCore::char16IndexToByteIndex( |
| 190 | UInput.getBuffer(), input.size(), index); |
| 191 | auto origMatchStart = stringCore::char16IndexToByteIndex( |
| 192 | UInput.getBuffer(), input.size(), matchStart); |
| 193 | auto origMatchEnd = stringCore::char16IndexToByteIndex( |
| 194 | UInput.getBuffer(), input.size(), matchEnd); |
| 195 | if (!matchLimited || results.size() < limit - 1) { |
| 196 | if (index == 0 && index == matchStart && matchStart == matchEnd) { |
| 197 | // no empty leading substring included for zero-width match |
| 198 | // at the beginning of the input char sequence. |
| 199 | continue; |
| 200 | } |
| 201 | results.emplace_back( |
| 202 | input.data() + origIndex, origMatchStart - origIndex); |
| 203 | index = matchEnd; |
| 204 | } else if (results.size() == limit - 1) { // last one |
| 205 | results.emplace_back(input.data() + origIndex, input.size() - origIndex); |
| 206 | index = matchEnd; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // If no match was found, return this |
| 211 | if (index == 0) { |
| 212 | results.clear(); |
| 213 | results.emplace_back(input); |
| 214 | } else { |
| 215 | // Add remaining segment |
| 216 | auto origIndex = stringCore::char16IndexToByteIndex( |
| 217 | UInput.getBuffer(), input.size(), index); |
| 218 | if (!matchLimited || results.size() < limit) |
| 219 | results.emplace_back(input.data() + origIndex, input.size() - origIndex); |
| 220 | |
| 221 | // Construct result |
| 222 | if (limit == 0) { |
| 223 | while (!results.empty() && results.back().empty()) { |