BioDynaMo  v1.05.117-b1949764
string.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_STRING_H_
16 #define CORE_UTIL_STRING_H_
17 
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 
22 namespace bdm {
23 
24 // https://stackoverflow.com/questions/874134/find-out-if-string-ends-with-another-string-in-c
25 inline bool EndsWith(const std::string& str, const std::string& suffix) {
26  return str.size() >= suffix.size() &&
27  str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
28 }
29 
30 inline bool StartsWith(const std::string& str, const std::string& prefix) {
31  return str.size() >= prefix.size() &&
32  str.compare(0, prefix.size(), prefix) == 0;
33 }
34 
35 std::vector<std::string> Split(const std::string& s,
36  const std::string& delimiter);
37 
38 namespace detail {
39 
44 inline void ConcatNextPart(std::ostringstream* ss) {}
45 
52 template <typename T, typename... Args>
53 inline void ConcatNextPart(std::ostringstream* ss, const T& arg,
54  const Args&... parts) {
55  *ss << arg;
56  ConcatNextPart(ss, parts...);
57 }
58 
59 } // namespace detail
60 
69 template <typename... Args>
70 inline std::string Concat(const Args&... parts) {
71  std::ostringstream message;
72  detail::ConcatNextPart(&message, parts...);
73  return message.str();
74 }
75 
76 } // namespace bdm
77 
78 #endif // CORE_UTIL_STRING_H_
bdm
Definition: agent.cc:39
bdm::StartsWith
bool StartsWith(const std::string &str, const std::string &prefix)
Definition: string.h:30
bdm::detail::ConcatNextPart
void ConcatNextPart(std::ostringstream *ss)
Appends the closing string to the message.
Definition: string.h:44
bdm::EndsWith
bool EndsWith(const std::string &str, const std::string &suffix)
Definition: string.h:25
bdm::Concat
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
Definition: string.h:70
bdm::Split
std::vector< std::string > Split(const std::string &s, const std::string &delimiter)
Definition: string.cc:21