1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #ifndef STLPLUS_STRING_UTILITIES #define STLPLUS_STRING_UTILITIES //////////////////////////////////////////////////////////////////////////////// // Author: Andy Rushton // Copyright: (c) Southampton University 1999-2004 // (c) Andy Rushton 2004 onwards // License: BSD License, see ../docs/license.html // Utilities for manipulating std::strings //////////////////////////////////////////////////////////////////////////////// #include "strings_fixes.hpp" #include "format_types.hpp" #include <vector> #include <string> #include <stdexcept> #include <time.h> namespace stlplus { //////////////////////////////////////////////////////////////////////////////// // Padding function allows a string to be printed in a fixed-width field //////////////////////////////////////////////////////////////////////////////// // The definitions for the alignment are declared in format_types.hpp // Any other value will cause std::invalid_argument to be thrown // exceptions: std::invalid_argument std:: string pad( const std:: string & str, alignment_t alignment, unsigned width, char padch = ' ' ); //////////////////////////////////////////////////////////////////////////////// // whitespace trimming //////////////////////////////////////////////////////////////////////////////// std:: string trim_left( const std:: string & val); std:: string trim_right( const std:: string & val); std:: string trim( const std:: string & val); //////////////////////////////////////////////////////////////////////////////// // case conversion for std::strings //////////////////////////////////////////////////////////////////////////////// std:: string lowercase( const std:: string & val); std:: string uppercase( const std:: string & val); //////////////////////////////////////////////////////////////////////////////// // character translation - inspired by Unix 'tr' command //////////////////////////////////////////////////////////////////////////////// // convert characters represented in from_set to the characters in the same position in to_set // for example: // filename = translate(filename,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // converts the filename to uppercase and returns the result (Note that the // uppercase function does this more easily). If the from_set is longer than // the to_set, then the overlap represents characters to delete (i.e. they map // to nothing) std:: string translate( const std:: string & input, const std:: string & from_set, const std:: string & to_set = std:: string ()); //////////////////////////////////////////////////////////////////////////////// // wildcard matching //////////////////////////////////////////////////////////////////////////////// // this function does wildcard matching of the wildcard expression against the candidate std::string // wildcards are NOT regular expressions // the wildcard characters are * and ? where * matches 1 or more characters and ? matches only one // there are also character sets [a-z] [qwertyuiop] etc. which match 1 character // TODO: character sets like [:alpha:] // TODO eventually: regular expression matching and substitution (3rd party library?) bool match_wildcard( const std:: string & wild, const std:: string & match); //////////////////////////////////////////////////////////////////////////////// // Perl-inspired split/join functions //////////////////////////////////////////////////////////////////////////////// // splits the string at every occurance of splitter and adds it as a separate string to the return value // the splitter is removed // a string with no splitter in it will give a single-value vector // an empty string gives an empty vector std:: vector <std:: string > split ( const std:: string & str, const std:: string & splitter = "\n" ); // the reverse of the above // joins the string vector to create a single string with the joiner inserted between the joins // Note: the joiner will not be added at the beginning or the end // However, there are optional fields to add such prefix and suffix strings std:: string join ( const std:: vector <std:: string >&, const std:: string & joiner = "\n" , const std:: string & prefix = "" , const std:: string & suffix = "" ); //////////////////////////////////////////////////////////////////////////////// // special displays //////////////////////////////////////////////////////////////////////////////// // display the parameter as a number in bytes, kbytes, Mbytes, Gbytes depending on range std:: string display_bytes( long bytes); // display the parameter in seconds as a string representation in weeks, days, hours, minutes, seconds // e.g. "1d 1:01:01" means 1 day, 1 hour, 1 minute and 1 second std:: string display_time( time_t seconds); //////////////////////////////////////////////////////////////////////////////// } // end namespace stlplus #endif |