1 /**
2  * \file modp_ascii.h
3  * \brief Simple ASCII manipulations including upper and lower casing,
4  *        white space trimming, and conversion to "printable" characters.
5  *
6  * blah blah blah
7  */
8 
9 /*
10  * <PRE>
11  * MODP_ASCII -- Simple ascii manipulation (uppercase, lowercase, etc)
12  * https://github.com/client9/stringencoders
13  *
14  * Copyright &copy; 2007-2016, Nick Galbreath -- nickg [at] client9 [dot] com
15  * All rights reserved.
16  *
17  * Released under MIT license.  See LICENSE for details.
18  * </PRE>
19  *
20  */
21 
22 #ifndef COM_MODP_STRINGENCODERS_ASCII
23 #define COM_MODP_STRINGENCODERS_ASCII
24 
25 #include "extern_c_begin.h"
26 #include "modp_stdint.h"
27 
28 /*
29  * \param[in,out] str the input string
30  * \param[in] len the length of input string (the strlen)
31  */
32 void modp_toupper(char* str, size_t len);
33 
34 /** \brief make lower case copy of input string
35  *
36  * \param[out] dest output buffer, with at least 'len + 1' bytes allocated
37  * \param[in] str the input string
38  * \param[in] len the length of input string (the strlen)
39  *
40  * Please make sure dest has been allocation with at least 'len+1'
41  * bytes.  This appends a trailing NULL character at the end of
42  * dest!
43  *
44  * This is based on the algorithm by Paul Hsieh
45  * http://www.azillionmonkeys.com/qed/asmexample.html
46  */
47 void modp_toupper_copy(char* dest, const char* str, size_t len);
48 
49 /** \brief lower case a string in place
50  *
51  * \param[in,out] str the input string
52  * \param[in] len the length of input string (the strlen)
53  *
54  */
55 void modp_tolower(char* str, size_t len);
56 
57 /** \brief make lower case copy of input string
58  *
59  * \param[out] dest output buffer, with at least 'len + 1' bytes allocated
60  * \param[in] str the input string
61  * \param[in] len the length of input string (the strlen)
62  *
63  * Please make sure dest has been allocation with at least 'len+1'
64  * bytes.  This appends a trailing NULL character at the end of
65  * dest!
66  *
67  * This is based on the algorithm by Paul Hsieh
68  * http://www.azillionmonkeys.com/qed/asmexample.html
69  */
70 void modp_tolower_copy(char* dest, const char* str, size_t len);
71 
72 /** \brief turn a string into 7-bit printable ascii.
73  *
74  * By "printable" we means all characters between 32 and 126.
75  * All other values are turned into '?'
76  *
77  * \param[in,out] str the input string
78  * \param[in] len the length of input string (the strlen)
79  *
80  */
81 void modp_toprint(char* str, size_t len);
82 
83 /** \brief make a printable copy of a string
84  *
85  * By "printable" we means all characters between 32 and 126.
86  * All other values are turned into '?'
87  *
88  * \param[out] dest output buffer, with at least 'len + 1' bytes allocated
89  * \param[in] str the input string
90  * \param[in] len the length of input string (the strlen)
91  *
92  * Please make sure dest has been allocation with at least 'len+1'
93  * bytes.  This appends a trailing NULL character at the end of
94  * dest!
95  */
96 void modp_toprint_copy(char* dest, const char* str, size_t len);
97 
98 /**
99  * \brief remove trailing whitespace from a string
100  * \param[in,out] str  string to be stripped
101  * \param[in] len the size of the input
102  * \return the size of the output, not including any ending null byte.
103  */
104 size_t modp_rtrim(char* str, size_t len);
105 
106 #include "extern_c_end.h"
107 
108 #ifdef __cplusplus
109 #include <string>
110 
111 namespace modp {
112 
toupper(std::string & str)113 inline std::string& toupper(std::string& str)
114 {
115     modp_toupper(const_cast<char*>(str.c_str()), str.size());
116     return str;
117 }
118 
toupper(const std::string & str)119 inline std::string toupper(const std::string& str)
120 {
121     std::string s(str.size(), '\0');
122     modp_toupper_copy(const_cast<char*>(s.data()), str.data(), str.size());
123     return s;
124 }
125 
tolower(const std::string & str)126 inline std::string tolower(const std::string& str)
127 {
128     std::string s(str.size(), '\0');
129     modp_tolower_copy(const_cast<char*>(s.data()), str.data(), str.size());
130     return s;
131 }
132 
tolower(std::string & str)133 inline std::string& tolower(std::string& str)
134 {
135     modp_tolower(const_cast<char*>(str.c_str()), str.size());
136     return str;
137 }
138 
toprint(const std::string & str)139 inline std::string toprint(const std::string& str)
140 {
141     std::string s(str.size(), '\0');
142     modp_toprint_copy(const_cast<char*>(s.data()), str.data(), str.size());
143     return s;
144 }
145 
toprint(std::string & str)146 inline std::string& toprint(std::string& str)
147 {
148     modp_toprint(const_cast<char*>(str.c_str()), str.size());
149     return str;
150 }
151 
rtrim(std::string & s)152 inline std::string& rtrim(std::string& s)
153 {
154     size_t d = modp_rtrim(const_cast<char*>(s.data()), s.size());
155     s.erase(d, std::string::npos);
156     return s;
157 }
158 }
159 
160 #endif /* __cplusplus */
161 
162 #endif /* MODP_ASCII */
163