PLaSK library
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#include "string.hpp"
15
16#include <boost/lexical_cast.hpp>
17
18namespace plask {
19
22
23std::string Printable::str() const {
24 return boost::lexical_cast<std::string>(*this);
25}
26
27std::pair<std::string, std::string> splitString2(const std::string& to_split, char splitter) {
28 std::string::size_type p = to_split.find(splitter);
29 return p == std::string::npos ?
30 std::pair<std::string, std::string>(to_split, "") :
31 std::pair<std::string, std::string>(to_split.substr(0, p), to_split.substr(p+1));
32}
33
34std::string removedChars(const std::string& str, const std::string& chars_to_remove) {
35 return filterChars(str, [&chars_to_remove](char c) { return chars_to_remove.find(c) == std::string::npos; });
36}
37
38split_esc_tokenizer splitEscIterator(const std::string& str, char splitter, char quote_char, char esc_char) {
39 return split_esc_tokenizer(str, boost::escaped_list_separator<char>(esc_char, splitter, quote_char));
40}
41
42std::vector<std::string> splitEsc(const std::string& str, char splitter, char quote_char, char esc_char) {
43 boost::escaped_list_separator<char> Separator(esc_char, splitter, quote_char);
44 boost::tokenizer< boost::escaped_list_separator<char> > tok( str, Separator );
45 return std::vector<std::string>(tok.begin(), tok.end());
46}
47
48inline bool isEngLower(char ch) {
49 return 'a' <= ch && ch <= 'z';
50}
51
52inline bool isEngUpper(char ch) {
53 return 'A' <= ch && ch <= 'Z';
54}
55
56bool isEngAlpha(char ch) { return isEngLower(ch) || isEngUpper(ch); }
57
58bool isDigit(char ch) { return '0' <= ch && ch <= '9'; }
59
60bool isCid(const char* potential_id) {
61 if (!isEngAlpha(*potential_id) && *potential_id != '_')
62 return false; //first should be letter or underline
63 for (++potential_id; *potential_id; ++potential_id) //all next, if are non NULL
64 if (!isEngAlpha(*potential_id) && !isDigit(*potential_id) && *potential_id != '_') //must be letter, digit or underline
65 return false;
66 return true;
67}
68
69} // namespace plask