OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
string_utilities.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// S T R I N G S
5//
6// Artificial Intelligence Techniques, SL
7// artelnics artelnics.com
8
9#pragma once
10
11#include "pch.h"
12
13namespace opennn
14{
15
20 [[nodiscard]] vector<string> get_tokens(const string&, const string&);
21
23 [[nodiscard]] vector<string_view> get_token_views(string_view, char);
24
26 [[nodiscard]] string_view trim_view(string_view);
27
29 [[nodiscard]] vector<string> tokenize(const string&);
30
32 [[nodiscard]] vector<string_view> tokenize_views(string_view);
33
35 [[nodiscard]] vector<string> convert_string_vector(const vector<vector<string>>&, const string&);
36
38 [[nodiscard]] bool is_numeric_string(string_view);
39
41 [[nodiscard]] bool is_date_time_string(string_view);
42
45
50 [[nodiscard]] time_t date_to_timestamp(const string&, Index = 0, const DateFormat& format = AUTO);
51
53 void replace_all_appearances(string&, const string&, const string&);
54
56 void replace_all_word_appearances(string&, const string&, const string&);
57
59 [[nodiscard]] string get_trimmed(const string&);
60
62 [[nodiscard]] bool has_numbers(const vector<string>&);
64 [[nodiscard]] bool has_numbers(const vector<string_view>&);
65
67 void replace(string&, const string&, const string&);
68
72 void display_progress_bar(int, int);
73
75 [[nodiscard]] string get_time(float);
76
78 [[nodiscard]] string get_first_word(const string&);
79
80 // Vector/tensor string conversion
81
83 template <typename T>
84 [[nodiscard]] string vector_to_string(const vector<T>& values, const string& separator = " ")
85 {
86 ostringstream buffer;
87
88 for (size_t i = 0; i < values.size(); ++i)
89 {
90 buffer << values[i];
91 if (i < values.size() - 1)
92 buffer << separator;
93 }
94
95 return buffer.str();
96 }
97
99 template <typename Derived>
100 [[nodiscard]] inline string vector_to_string(const Eigen::DenseBase<Derived>& values, const string& separator = " ")
101 {
102 ostringstream buffer;
103 for (Index i = 0; i < values.size(); ++i) buffer << values(i) << separator;
104 return buffer.str();
105 }
106
108 void string_to_vector(const string& input, VectorR& values);
109
111 template <typename T, size_t Rank>
112 [[nodiscard]] string tensor_to_string(const TensorR<Rank>& values, const string& separator = " ")
113 {
114 ostringstream buffer;
115
116 for (Index i = 0; i < values.size(); ++i)
117 buffer << values(i) << separator;
118
119 return buffer.str();
120 }
121
123 template <typename T, size_t Rank>
124 void string_to_tensor(const string& input, TensorR<Rank>& values)
125 {
126 istringstream stream(input);
127 T value;
128 Index i = 0;
129
130 while (stream >> value)
131 values(i++) = value;
132 }
133
135 [[nodiscard]] bool contains(const vector<string>&, const string&);
137 [[nodiscard]] bool contains(const vector<string>&, string_view);
138}
139
140// OpenNN: Open Neural Networks Library.
141// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
142// Licensed under the GNU Lesser General Public License v2.1 or later.
Definition adaptive_moment_estimation.h:14
vector< string_view > get_token_views(string_view, char)
Splits a string view on the given separator, returning views into the original buffer.
string tensor_to_string(const TensorR< Rank > &values, const string &separator=" ")
Serializes a tensor's flat data to a string with the given separator.
Definition string_utilities.h:112
bool contains(const vector< string > &, const string &)
Returns true if the vector contains the given element.
void display_progress_bar(int, int)
Prints a textual progress bar to stdout for an in-progress operation.
string get_first_word(const string &)
Returns the first whitespace-delimited word of a string.
vector< string > get_tokens(const string &, const string &)
Splits a string on every occurrence of any character in the separator set.
void string_to_tensor(const string &input, TensorR< Rank > &values)
Parses a whitespace-separated string into the flat storage of a tensor.
Definition string_utilities.h:124
string vector_to_string(const vector< T > &values, const string &separator=" ")
Serializes a vector to a string with the given element separator.
Definition string_utilities.h:84
string get_time(float)
Formats a duration in seconds as a human-readable HH:MM:SS string.
string_view trim_view(string_view)
Returns a view onto the input with leading and trailing whitespace removed.
vector< string > tokenize(const string &)
Splits the input on whitespace into individual tokens.
time_t date_to_timestamp(const string &, Index=0, const DateFormat &format=AUTO)
Parses a date/time string into a Unix timestamp.
vector< string > convert_string_vector(const vector< vector< string > > &, const string &)
Joins each inner vector with the separator, returning one flattened string per row.
string get_trimmed(const string &)
Returns a copy of the string with leading and trailing whitespace removed.
vector< string_view > tokenize_views(string_view)
Whitespace-tokenises a string view, returning views into the source buffer.
void replace_all_appearances(string &, const string &, const string &)
Replaces every occurrence of a substring with another, in place.
void replace_all_word_appearances(string &, const string &, const string &)
Replaces every whole-word occurrence of a token with another, in place.
bool has_numbers(const vector< string > &)
Returns true if any element of the vector parses as a number.
bool is_date_time_string(string_view)
Returns true if the string matches one of the supported date/time formats.
void string_to_vector(const string &input, VectorR &values)
Parses a whitespace-separated string of floats into a VectorR.
DateFormat
Order of the day, month, and year fields in a date string (AUTO probes the input).
Definition string_utilities.h:44
@ YMD
Definition string_utilities.h:44
@ AUTO
Definition string_utilities.h:44
@ DMY
Definition string_utilities.h:44
@ MDY
Definition string_utilities.h:44
bool is_numeric_string(string_view)
Returns true if the string can be parsed as a numeric literal.
void replace(string &, const string &, const string &)
In-place replacement of every occurrence of a substring with another.
Matrix< float, Dynamic, 1 > VectorR
Definition pch.h:181
Tensor< float, Rank, Layout|AlignedMax > TensorR
Definition pch.h:194