DPC++ Runtime
Runtime libraries for oneAPI DPC++
split_string.hpp
Go to the documentation of this file.
1 //==------------ split_string.hpp ------------------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <string>
12 #include <string_view>
13 #include <vector>
14 
15 namespace sycl {
16 inline namespace _V1 {
17 namespace detail {
18 inline std::vector<std::string> split_string(std::string_view str,
19  char delimeter) {
20  std::vector<std::string> Result;
21  size_t Start = 0;
22  size_t End = 0;
23  while ((End = str.find(delimeter, Start)) != std::string::npos) {
24  Result.emplace_back(str.substr(Start, End - Start));
25  Start = End + 1;
26  }
27  // Get the last substring and ignore the null character so we wouldn't get
28  // double null characters \0\0 at the end of the substring
29  End = str.find('\0');
30  if (Start < End) {
31  std::string LastSubStr(str.substr(Start, End - Start));
32  // In case str has a delimeter at the end, the substring will be empty, so
33  // we shouldn't add it to the final vector
34  if (!LastSubStr.empty())
35  Result.push_back(LastSubStr);
36  }
37  return Result;
38 }
39 } // namespace detail
40 } // namespace _V1
41 } // namespace sycl
std::vector< std::string > split_string(std::string_view str, char delimeter)
Definition: access.hpp:18