Keyple Util C++ Library 2.0.0
Reference Terminal Reader API for C++
StringUtils.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional information regarding *
5 * copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the terms of the Eclipse *
8 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0 *
9 * *
10 * SPDX-License-Identifier: EPL-2.0 *
11 **************************************************************************************************/
12
13#pragma once
14
15#include <cstdarg>
16#include <regex>
17#include <string>
18
19namespace keyple {
20namespace core {
21namespace util {
22namespace cpp {
23
25public:
26 static inline const std::string format(const char* format, ...)
27 {
28 char buf[1024];
29 va_list args;
30
31 va_start(args, format);
32 vsprintf(buf, format, args);
33 va_end(args);
34
35 return buf;
36 }
37
38 static inline bool contains(const std::string& s1, const std::string& s2)
39 {
40 return s1.find(s2) != std::string::npos;
41 }
42
43 static inline bool matches(const std::string& s, const std::string& regex)
44 {
45 std::regex r(regex);
46
47 return std::regex_match(s, r);
48 }
49
50 static inline const std::vector<std::string> split(const std::string& string,
51 const std::string& delimiter)
52 {
53 std::vector<std::string> tokens;
54 std::string s = string;
55
56 size_t pos = 0;
57
58 while ((pos = s.find(delimiter)) != std::string::npos) {
59 tokens.push_back(s.substr(0, pos));
60 s.erase(0, pos + delimiter.length());
61 }
62
63 return tokens;
64 }
65};
66
67}
68}
69}
70}
static const std::vector< std::string > split(const std::string &string, const std::string &delimiter)
Definition: StringUtils.h:50
static bool contains(const std::string &s1, const std::string &s2)
Definition: StringUtils.h:38
static bool matches(const std::string &s, const std::string &regex)
Definition: StringUtils.h:43
static const std::string format(const char *format,...)
Definition: StringUtils.h:26