Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
LUPI::Misc Namespace Reference

Functions

std::string stripString (std::string const &a_string, bool a_left=true, bool a_right=true)
std::vector< std::string > splitString (std::string const &a_string, char a_delimiter, bool a_strip=false)
std::vector< std::string > splitString (std::string const &a_string, std::string const &a_delimiter, bool a_strip=false)
std::string joinStrings (std::string const &a_sep, std::vector< std::string > a_strings)
std::string replaceString (std::string const &a_string, std::string const &a_old, std::string const &a_new, bool a_all)
std::vector< std::string > splitXLinkString (std::string const &a_string)
bool stringToInt (std::string const &a_string, int &a_value)
bool stringToSize_t (std::string const &a_string, std::size_t &a_value)
std::string argumentsToString (char const *a_format,...)
std::string doubleToString3 (char const *a_format, double a_value, bool a_reduceBits=false)
std::string doubleToShortestString (double a_value, int a_significantDigits=15, int a_favorEFormBy=0)
void printCommand (std::string const &a_indent, int a_argc, char **a_argv)

Function Documentation

◆ argumentsToString()

std::string LUPI::Misc::argumentsToString ( char const * a_format,
... )

Returns a string that represent the arguments formatted per a_format.

Parameters
a_format[in] A printf like format specifier for converting a double to a string.
Returns
The string representing the arguments formatted per a_format.

Definition at line 305 of file LUPI_misc.cc.

305 {
306
307 va_list args;
308
309 va_start( args, a_format );
310 char *charStr = smr_vallocateFormatMessage( a_format, &args );
311 va_end( args );
312
313 std::string string( charStr );
314
315 free( charStr );
316 return( string );
317}
void free(voidpf ptr)
char * smr_vallocateFormatMessage(char const *fmt, va_list *args)

Referenced by doubleToString3(), PoPI::ParseIntidInfo::id(), PoPI::Database::indexFromIntid(), GIDI::intToString(), GIDI::LLNL_fidToLabel(), GIDI::LLNL_gidToLabel(), GIDI::Transporting::Flux_order::print(), GIDI::Transporting::MultiGroup::print(), MCGIDI::ContinuousEnergyGain::print(), MCGIDI::DomainHash::print(), MCGIDI::HeatedCrossSectionContinuousEnergy::print(), MCGIDI::HeatedCrossSectionsContinuousEnergy::print(), MCGIDI::HeatedReactionCrossSectionContinuousEnergy::print(), MCGIDI::NuclideGammaBranchInfo::print(), MCGIDI::NuclideGammaBranchStateInfo::print(), PoPI::Database::print(), PoPI::ParseIdInfo::print(), GIDI::RISI::Reaction::printAsRIS_file(), GIDI::size_t_ToString(), PoPI::ChemicalElement::toXMLList(), PoPI::Isotope::toXMLList(), PoPI::MetaStable::toXMLList(), PoPI::PQ_double::valueToString(), PoPI::PQ_integer::valueToString(), MCGIDI::HeatedReactionCrossSectionMultiGroup::write(), and MCGIDI::MultiGroupGain::write().

◆ doubleToShortestString()

std::string LUPI::Misc::doubleToShortestString ( double a_value,
int a_significantDigits,
int a_favorEFormBy )

Returns a string representation of a_value that contains the smallest number of character yet still agrees with a_value to a_significantDigits significant digits. For example, for a_value = 1.20000000001, "1.2" will be returned if a_significantDigits is less than 11, otherwise "1.20000000001" is returned.

Parameters
a_value[in/out] The double to convert to a string.
a_significantDigits[in] The number of significant digits the string representation should agree with the double.
a_favorEFormBy[in] The bigger this value the more likely an e-form will be favored in the string representation.
Returns
A std::string instance.

Definition at line 349 of file LUPI_misc.cc.

349 {
350
351 char *charValue = nf_floatToShortestString( a_value, a_significantDigits, a_favorEFormBy, nf_floatToShortestString_trimZeros );
352
353 std::string stringValue( charValue );
354 free( charValue );
355
356 return( stringValue );
357}
#define nf_floatToShortestString_trimZeros
char * nf_floatToShortestString(double value, int significantDigits, int favorEFormBy, int flags)

Referenced by GIDI::doublesToXMLList(), GIDI::nodeWithValuesToDoubles(), GIDI::RISI::Reaction::printAsRIS_file(), GIDI::ACE_URR::IncidentEnergy::toXMLList(), GIDI::AxisDomain::toXMLList(), GIDI::PhysicalQuantity::toXMLList(), GIDI::Styles::CoulombPlusNuclearElasticMuCutoff::toXMLList(), GIDI::TargetInfo::Nuclide::toXMLList(), GIDI::Functions::Constant1d::toXMLList_func(), GIDI::Functions::DiscreteGamma2d::toXMLList_func(), GIDI::Functions::Gridded1d::toXMLList_func(), GIDI::Functions::Legendre1d::toXMLList_func(), GIDI::Functions::Polynomial1d::toXMLList_func(), GIDI::Functions::PrimaryGamma2d::toXMLList_func(), GIDI::Functions::Regions1d::toXMLList_func(), GIDI::Functions::Regions2d::toXMLList_func(), GIDI::Functions::Unspecified1d::toXMLList_func(), GIDI::Functions::Xs_pdf_cdf1d::toXMLList_func(), GIDI::Functions::XYs1d::toXMLList_func(), GIDI::Functions::XYs2d::toXMLList_func(), and GIDI::Functions::XYs3d::toXMLList_func().

◆ doubleToString3()

std::string LUPI::Misc::doubleToString3 ( char const * a_format,
double a_value,
bool a_reduceBits )

Returns a string that represent the double a_value using a printf like format specifier.

Parameters
a_format[in] A printf like format specifier for converting a double to a string.
a_value[in] The double to be converted to a string.
a_reduceBits[in] If true the lowest digit or two are altered in an attempt to convert numbers like 4.764999999999999 and 4.765 to the same string.

Definition at line 327 of file LUPI_misc.cc.

327 {
328
329
330 if( a_reduceBits ) { // The next line is an attempt to convert numbers like 4.764999999999999 and 4.765 to the same value.
331 a_value = std::stod( LUPI::Misc::argumentsToString("%.14e", a_value ) );
332 }
333
334 return( LUPI::Misc::argumentsToString( a_format, a_value ) );
335}
std::string argumentsToString(char const *a_format,...)
Definition LUPI_misc.cc:305

Referenced by MCGIDI::HeatedCrossSectionContinuousEnergy::print(), and MCGIDI::HeatedCrossSectionsContinuousEnergy::sampleReaction().

◆ joinStrings()

std::string LUPI::Misc::joinStrings ( std::string const & a_sep,
std::vector< std::string > a_strings )

This function adds together the strings in a_strings with a_sep between the strings in a_strings.

Parameters
a_delimiter[in] The delimiter string.
a_strings[in] The string to split.
Returns
A std::string.

Definition at line 158 of file LUPI_misc.cc.

158 {
159
160 std::string string;
161 std::string sep = "";
162 std::string const *sepPointer = &sep;
163
164 for( auto iter = a_strings.begin( ); iter != a_strings.end( ); ++iter ) {
165 string += *sepPointer + *iter;
166 sepPointer = &a_sep;
167 }
168
169 return( string );
170}

◆ printCommand()

void LUPI::Misc::printCommand ( std::string const & a_indent,
int a_argc,
char ** a_argv )

For internal use only.

Parameters
a_indent[in] A string containing the help line for an argument up to the description string.
a_argc[in] The number of command arguments.
a_argv[in] The list of command arguments.

Definition at line 367 of file LUPI_misc.cc.

367 {
368
369 std::cout << a_indent << a_argv[0];
370 for( int iargc = 1; iargc < a_argc; ++iargc ) std::cout << " " << a_argv[iargc];
371 std::cout << std::endl;
372}

◆ replaceString()

std::string LUPI::Misc::replaceString ( std::string const & a_string,
std::string const & a_old,
std::string const & a_new,
bool a_all )

This function replace one (or all if a_all is true) occurrence(s) of a_old in a_string with a_new.

Parameters
a_string[in] The string to split.
a_old[in] The current sub-string in a_string that is replaced by a_new.
a_new[in] The new sub-string that replace a_old..
a_all[in] If true all occurrence of a_old are replaced by a_new; otherwise, only the first occurrence is replaced.
Returns
A std::string.

Definition at line 183 of file LUPI_misc.cc.

183 {
184
185 std::string string( a_string );
186
187 while( true ) {
188 std::size_t index = string.find( a_old );
189 if( index == std::string::npos ) break;
190 string.replace( index, a_old.size( ), a_new );
191 if( !a_all ) break;
192 }
193
194 return( string );
195}

◆ splitString() [1/2]

std::vector< std::string > LUPI::Misc::splitString ( std::string const & a_string,
char a_delimiter,
bool a_strip )

This function splits that string a_string into separate strings using the delimiter character a_delimiter. If the delimiter is the space character, consecutive spaces are treated as one space, and leading and trailing white spaces are ignored.

Parameters
a_string[in] The string to split.
a_delimiter[in] The delimiter character.
a_strip[in] If true, white spaces are removed from the begining and ending of each string in the list returned.
Returns
The list of strings.

Definition at line 103 of file LUPI_misc.cc.

103 {
104
105 std::stringstream stringStream( a_string );
106 std::string segment;
107 std::vector<std::string> segments;
108
109 while( std::getline( stringStream, segment, a_delimiter ) ) {
110 if( ( a_delimiter == ' ' ) && ( segment.size( ) == 0 ) ) continue;
111
112 if( a_strip ) segment = stripString( segment );
113 segments.push_back( segment );
114 }
115
116 return( segments );
117}
std::string stripString(std::string const &a_string, bool a_left=true, bool a_right=true)
Definition LUPI_misc.cc:68

Referenced by GIDI::RISI::Protare::addReaction(), GIDI::FlattenedArrayData::FlattenedArrayData(), MCGIDI::GRIN_captureLevelProbability::GRIN_captureLevelProbability(), MCGIDI::GRIN_levelsAndProbabilities::GRIN_levelsAndProbabilities(), PoPI::ParseIdInfo::ParseIdInfo(), and LUPI::FormatVersion::setFormat().

◆ splitString() [2/2]

std::vector< std::string > LUPI::Misc::splitString ( std::string const & a_string,
std::string const & a_delimiter,
bool a_strip )

This function splits that string a_string into separate strings using the delimiter string a_delimiter.

Parameters
a_string[in] The string to split.
a_delimiter[in] The delimiter string.
a_strip[in] If true, white spaces are removed from the begining and ending of each string in the list returned.
Returns
The list of strings.

Definition at line 129 of file LUPI_misc.cc.

129 {
130
131 std::string segment;
132 std::vector<std::string> segments;
133
134 for( std::size_t index1 = 0; ; ) {
135 std::size_t index2 = a_string.find( a_delimiter, index1 );
136
137 segment = a_string.substr( index1, index2 - index1 );
138
139 if( a_strip ) segment = stripString( segment );
140 segments.push_back( segment );
141 if( index2 == std::string::npos ) break;
142
143 index1 = index2 + a_delimiter.size( );
144 }
145
146 return( segments );
147}

◆ splitXLinkString()

std::vector< std::string > LUPI::Misc::splitXLinkString ( std::string const & a_XLink)

This function splits that string a_string into separate strings using the delimiter character "/" as for a XLink. The delimiter character "/"'s in each quoted region of the string is not split.

Parameters
a_string[in] The XLink string to split.
Returns
The XLink parts as a list of strings.

Definition at line 206 of file LUPI_misc.cc.

206 {
207
208 char quote = ' ';
209 std::vector<std::string> elements;
210
211 std::size_t start = 0;
212
213 while( a_XLink[start] == '/' ) ++start;
214
215 std::size_t end = start;
216 std::size_t size = a_XLink.size( );
217
218 if( start != 0 ) {
219 elements.push_back( "" );
220 }
221
222 for( ; end < size ; ++end ) {
223 char current = a_XLink[end];
224 if( quote != ' ' ) { // Are we inside a quote?
225 if( current == quote ) quote = ' ';
226 continue;
227 }
228
229 if( ( current == '\'' ) || ( current == '"' ) ) { // Are we starting a quote?
230 quote = current;
231 continue;
232 }
233
234 if( current == '/' ) {
235 std::string element = a_XLink.substr( start, end - start );
236 elements.push_back( std::move( element ) );
237 while( a_XLink[end] == '/' ) ++end;
238 start = end;
239 if( end == size ) break; // Happens when XLink ends with '/'.
240 }
241 }
242
243 if( start < end ) {
244 std::string element = a_XLink.substr( start, end - start );
245 elements.push_back( element );
246 }
247
248 return( elements );
249}

Referenced by GUPI::Ancestry::findInAncestry(), and GUPI::Ancestry::findInAncestry().

◆ stringToInt()

bool LUPI::Misc::stringToInt ( std::string const & a_string,
int & a_value )

Converts a string to an integer. All characteros of the string must be valid int characters except for the trailing 0.

Parameters
a_string[in] The string to convert to an int.
a_value[in] The converted int value.
Returns
true if successful and false otherwise.

Definition at line 260 of file LUPI_misc.cc.

260 {
261
262 char const *digits = a_string.c_str( );
263 char *nonDigit;
264 long value = strtol( digits, &nonDigit, 10 );
265
266 if( digits == nonDigit ) return( false );
267 if( *nonDigit != 0 ) return( false );
268 if( ( value < INT_MIN ) || ( value > INT_MAX ) ) return( false );
269
270 a_value = static_cast<int>( value );
271 return( true );
272}
#define INT_MIN
Definition templates.hh:94
#define INT_MAX
Definition templates.hh:90

Referenced by PoPI::ParseIdInfo::ParseIdInfo(), and LUPI::FormatVersion::setFormat().

◆ stringToSize_t()

bool LUPI::Misc::stringToSize_t ( std::string const & a_string,
std::size_t & a_value )

Converts a string to an integer. All characteros of the string must be valid int characters except for the trailing 0.

Parameters
a_string[in] The string to convert to an int.
a_value[in] The converted int value.
Returns
true if successful and false otherwise.

Definition at line 283 of file LUPI_misc.cc.

283 {
284
285 char const *digits = a_string.c_str( );
286 char *nonDigit;
287 long value = strtol( digits, &nonDigit, 10 );
288
289 if( digits == nonDigit ) return( false );
290 if( *nonDigit != 0 ) return( false );
291 if( ( value < 0 ) || ( value > LONG_MAX ) ) return( false );
292
293 a_value = static_cast<std::size_t>( value );
294 return( true );
295}

◆ stripString()

std::string LUPI::Misc::stripString ( std::string const & a_string,
bool a_left,
bool a_right )

This returns a copy of a_string with its leading (if a_left is true) and trailing (if a_left is true) white spaces removed.

Parameters
a_string[in] The string to copy and strip leading and trailing white spaces from.
a_left[in] If true, white spaces are removed from the beginning of the string.
a_right[in] If true, white spaces are removed from the ending of the string.
Returns
The list of strings.

Definition at line 68 of file LUPI_misc.cc.

68 {
69
70 std::string stripped( a_string );
71 std::string::iterator beginning = stripped.begin( ), ending = stripped.end( );
72
73 if( a_left ) {
74 for( ; beginning != ending; ++beginning )
75 if( !std::isspace( *beginning ) ) break;
76 }
77
78 if( ( beginning != ending ) && a_right ) {
79 --ending;
80 for( ; beginning != ending; --ending )
81 if( !std::isspace( *ending ) ) break;
82 ++ending;
83 }
84
85 stripped.erase( ending, stripped.end( ) );
86 stripped.erase( stripped.begin( ), beginning );
87
88 return( stripped );
89}

Referenced by MCGIDI::GRIN_levelsAndProbabilities::GRIN_levelsAndProbabilities(), splitString(), and splitString().