cgv
Loading...
Searching...
No Matches
token.h
1#pragma once
2
3#include <algorithm>
4#include <string>
5#include <iostream>
6#include <vector>
7
8#include "lib_begin.h"
9
10namespace cgv {
11 namespace utils {
12
17struct CGV_API token
18{
20 const char* begin, *end;
22 token();
24 token(const char* _str);
26 token(const char* _b, const char* _e);
28 token(const std::string& s);
30 size_t get_length() const;
32 size_t size() const;
34 bool empty() const;
36 void skip(const std::string& skip_chars);
38 void reverse_skip(const std::string& skip_chars);
40 char operator [] (unsigned int i) const;
42 bool operator == (const char* s) const;
44 bool operator == (const std::string& s) const;
46 bool operator != (const char* s) const;
48 bool operator != (const std::string& s) const;
49};
50
52inline size_t token::get_length() const { return end-begin; }
54inline size_t token::size() const { return end-begin; }
56inline bool token::empty() const { return begin == end; }
58inline char token::operator [] (unsigned int i) const { return begin[i]; }
60inline std::string to_string(const token& t) { return t.empty()?std::string():std::string(t.begin, t.get_length()); }
62inline std::vector<std::string> to_strings(const std::vector<token>::const_iterator first, const std::vector<token>::const_iterator last) {
63 std::vector<std::string> strs;
64 std::transform(first, last, std::back_inserter(strs), [](const cgv::utils::token& t) { return to_string(t); });
65 return strs;
66}
68inline std::vector<std::string> to_strings(const std::vector<token>& ts) {
69 return to_strings(ts.begin(), ts.end());
70}
72inline std::ostream& operator << (std::ostream& os, const token& t) { if (!t.empty()) os << std::string(t.begin, t.get_length()); return os; }
73
74 }
75}
76
77#include <cgv/config/lib_end.h>
std::vector< std::string > to_strings(const std::vector< token >::const_iterator first, const std::vector< token >::const_iterator last)
convert to strings
Definition token.h:62
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
the cgv namespace
Definition print.h:11
representation of a token in a text by two pointers begin and end, that point to the first character ...
Definition token.h:18
bool empty() const
return whether the token is empty
Definition token.h:56
size_t get_length() const
return the length of the token in number of characters
Definition token.h:52
char operator[](unsigned int i) const
return the i-th character of the token
Definition token.h:58
const char * begin
pointers that define the range of characters
Definition token.h:20
size_t size() const
return the length of the token in number of characters
Definition token.h:54