String Manipulation¶
There are a couple classes and functions to help manipulate strings of
text found in src/base/string_conv.h
.
The columnify class converts a set of strings into
nicely formatted columns by padding with the necessary amount of
spaces. This class operates on string objects of type std::string
,
and also works will for formatting columns of floating-point numbers.
This class is used to provide output for matrices in the functions
o2scl::matrix_out()
.
The format_float class will reformat double precision numbers into a form appropriate for HTML or LaTeX documents.
A related function, o2scl::screenify()
, reformats a column
of strings into many columns stored row-by-row in a new string array.
It operates very similar to the way the classic Unix command ls
organizes files and directories in multiple columns in order to save
screen space.
The function o2scl::function_to_double()
converts strings
like "pi/3.0"
and "exp(cos(-1.0e-2))"
to double-precision
floating point numbers using find_constants
and calc_utf8. An alternate version which won’t
call the error handler is
o2scl::function_to_double_nothrow()
. This latter function
is the one used by acol -calc
. These conversions also work in
multiprecision, see Multiprecision Support.
A class kwargs stores a map of strings which can be converted to base C types similar to a Python dictionary.
There are also a set of string conversion functions. They partially
duplicate the functionality of C++ Standard Library functions like
std::stod()
and std::to_string
, but often provide alternate
forms and different options. These functions include:
o2scl::btos()
- boolean value to stringo2scl::dtos()
- double to stringo2scl::itos()
- integer to stringo2scl::ptos()
- pointer to stringo2scl::szttos()
-size_t
to stringo2scl::stob()
- string to boolean valueo2scl::stod()
- string to double (usesstd::stod()
)o2scl::stoi()
- string to integer (usesstd::stoi()
)o2scl::stoszt()
- string tosize_t
o2scl::s32tod_nothrow()
- char32 string to double which never throws an exceptiono2scl::stod_nothrow()
- string to double which never throws an exceptiono2scl::stoi_nothrow()
- string to integer which never throws an exceptiono2scl::stoszt_nothrow()
-size_t
to string which never throws an exception
Other functions in src/base/string_conv.h
are:
String example¶
/* Example: ex_string.cpp
-------------------------------------------------------------------
Demonstrate the string conversion classes and functions
*/
#include <o2scl/test_mgr.h>
#include <o2scl/format_float.h>
#include <o2scl/misc.h>
#include <o2scl/funct_to_fp.h>
using namespace std;
using namespace o2scl;
int main(void) {
cout.setf(ios::scientific);
test_mgr t;
t.set_output_level(2);
format_float ff;
ff.latex_mode();
cout << "Output a LaTeX table of numbers: " << endl;
cout << endl;
cout << "\\begin{table}" << endl;
cout << "\\begin{tabular}{cc}" << endl;
cout << "\\hline" << endl;
cout << "Column 1 & Column 2 \\\\" << endl;
cout << "\\hline" << endl;
cout << ff.convert(1.1e-6) << " & "
<< ff.convert(-4.0) << " \\\\" << endl;
cout << ff.convert(134.567) << " & "
<< ff.convert(1568234034) << " \\\\" << endl;
cout << "\\hline" << endl;
cout << "\\end{tabular}" << endl;
cout << "\\end{table}" << endl;
cout << endl;
cout << "Function function_to_double():" << endl;
double x=function_to_double("cyl_bessel_i(2,pi)");
cout << x << endl;
t.test_rel(x,2.618495,1.0e-6,"BesselI(2,x)");
cout << "Function string_to_uint_list():\n " << endl;
vector<size_t> ulist;
string_to_uint_list("1,2-4,6,10-11",ulist);
vector_out(cout,ulist,true);
t.test_gen(ulist==vector<size_t>({1,2,3,4,6,10,11}),"uint list");
t.report();
return 0;
}