apt: apt-pkg/contrib/strutl.h Source File

apt 2.3.15 commandline package manager

strutl.h
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 String Util - These are some useful string functions
6
7 _strstrip is a function to remove whitespace from the front and end
8 of a string.
9
10 This source is placed in the Public Domain, do with it what you will
11 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
12
13 ##################################################################### */
14 /*}}}*/
15#ifndef STRUTL_H
16#define STRUTL_H
17
18#include <cstring>
19#include <iostream>
20#include <limits>
21#include <string>
22#include <vector>
23#include <apt-pkg/string_view.h>
24#include <stddef.h>
25#include <time.h>
26
27#include "macros.h"
28
29
30namespace APT {
31 namespace String {
32 APT_PUBLIC std::string Strip(const std::string &s);
33 APT_PUBLIC bool Endswith(const std::string &s, const std::string &ending);
34 APT_PUBLIC bool Startswith(const std::string &s, const std::string &starting);
35 APT_PUBLIC std::string Join(std::vector<std::string> list, const std::string &sep);
36 // Returns string display length honoring multi-byte characters
37 APT_PUBLIC size_t DisplayLength(StringView str);
38 }
39}
40
41
42APT_PUBLIC bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
43APT_PUBLIC char *_strstrip(char *String);
44APT_PUBLIC char *_strrstrip(char *String); // right strip only
45APT_DEPRECATED_MSG("Use SubstVar to avoid memory headaches") APT_PUBLIC char *_strtabexpand(char *String,size_t Len);
46APT_PUBLIC bool ParseQuoteWord(const char *&String,std::string &Res);
47APT_PUBLIC bool ParseCWord(const char *&String,std::string &Res);
48APT_PUBLIC std::string QuoteString(const std::string &Str,const char *Bad);
49APT_PUBLIC std::string DeQuoteString(const std::string &Str);
50APT_PUBLIC std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
51
52// unescape (\0XX and \xXX) from a string
53APT_PUBLIC std::string DeEscapeString(const std::string &input);
54
55APT_PUBLIC std::string SizeToStr(double Bytes);
56APT_PUBLIC std::string TimeToStr(unsigned long Sec);
57APT_PUBLIC std::string Base64Encode(const std::string &Str);
58APT_PUBLIC std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
59APT_PUBLIC std::string URItoFileName(const std::string &URI);
69APT_PUBLIC std::string TimeRFC1123(time_t Date, bool const NumericTimezone);
85APT_PUBLIC bool RFC1123StrToTime(const std::string &str,time_t &time) APT_MUSTCHECK;
86APT_PUBLIC bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
87APT_PUBLIC std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
88APT_PUBLIC int StringToBool(const std::string &Text,int Default = -1);
89APT_PUBLIC bool ReadMessages(int Fd, std::vector<std::string> &List);
90APT_PUBLIC bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
91APT_PUBLIC bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
92APT_PUBLIC bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
93APT_PUBLIC bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
94APT_PUBLIC bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length);
95// input changing string split
96APT_PUBLIC bool TokSplitString(char Tok,char *Input,char **List,
97 unsigned long ListMax);
98
99// split a given string by a char
100APT_PUBLIC std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
101
102/* \brief Return a vector of strings from string "input" where "sep"
103 * is used as the delimiter string.
104 *
105 * \param input The input string.
106 *
107 * \param sep The separator to use.
108 *
109 * \param maxsplit (optional) The maximum amount of splitting that
110 * should be done .
111 *
112 * The optional "maxsplit" argument can be used to limit the splitting,
113 * if used the string is only split on maxsplit places and the last
114 * item in the vector contains the remainder string.
115 */
116APT_PUBLIC std::vector<std::string> StringSplit(std::string const &input,
117 std::string const &sep,
118 unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_PURE;
119
120
121APT_HIDDEN bool iovprintf(std::ostream &out, const char *format, va_list &args, ssize_t &size);
122APT_PUBLIC void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
123APT_PUBLIC void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
124APT_PUBLIC char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
125APT_PUBLIC bool CheckDomainList(const std::string &Host, const std::string &List);
126
127/* Do some compat mumbo jumbo */
128#define tolower_ascii tolower_ascii_inline
129#define isspace_ascii isspace_ascii_inline
130
131APT_PURE APT_HOT
132static inline int tolower_ascii_unsafe(int const c)
133{
134 return c | 0x20;
135}
136APT_PURE APT_HOT
137static inline int tolower_ascii_inline(int const c)
138{
139 return (c >= 'A' && c <= 'Z') ? c + 32 : c;
140}
141APT_PURE APT_HOT
142static inline int isspace_ascii_inline(int const c)
143{
144 // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' '
145 return (c >= 9 && c <= 13) || c == ' ';
146}
147APT_PURE APT_HOT
148static inline int islower_ascii(int const c)
149{
150 return c >= 'a' && c <= 'z';
151}
152APT_PURE APT_HOT
153static inline int isupper_ascii(int const c)
154{
155 return c >= 'A' && c <= 'Z';
156}
157APT_PURE APT_HOT
158static inline int isalpha_ascii(int const c)
159{
160 return isupper_ascii(c) || islower_ascii(c);
161}
162
163APT_PUBLIC std::string StripEpoch(const std::string &VerStr);
164
165#define APT_MKSTRCMP(name,func) \
166inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
167inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
168inline APT_PURE int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));} \
169inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());} \
170inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);}
171
172#define APT_MKSTRCMP2(name,func) \
173inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
174inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
175inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
176inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
177
178APT_PUBLIC int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
179APT_PUBLIC int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
180
181/* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
182 case the definition of string::const_iterator is not the same as
183 const char * and we need these extra functions */
184#if __GNUC__ >= 3
185APT_PUBLIC int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
186 const char *B,const char *BEnd);
187APT_PUBLIC int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
188 std::string::const_iterator B,std::string::const_iterator BEnd);
189APT_PUBLIC int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
190 const char *B,const char *BEnd);
191APT_PUBLIC int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
192 std::string::const_iterator B,std::string::const_iterator BEnd);
193
194inline APT_PURE int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));}
195inline APT_PURE int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));}
196#endif
197
198APT_MKSTRCMP2(stringcmp,stringcmp)
199APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
200
201// Return the length of a NULL-terminated string array
202APT_PUBLIC size_t APT_PURE strv_length(const char **str_array);
203
204
205inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
206
207 class APT_PUBLIC URI
208{
209 void CopyFrom(const std::string &From);
210
211 public:
212
213 std::string Access;
214 std::string User;
215 std::string Password;
216 std::string Host;
217 std::string Path;
218 unsigned int Port;
219
220 operator std::string();
221 inline void operator =(const std::string &From) {CopyFrom(From);}
222 inline bool empty() {return Access.empty();};
223 static std::string SiteOnly(const std::string &URI);
224 static std::string ArchiveOnly(const std::string &URI);
225 static std::string NoUserPassword(const std::string &URI);
226
227 explicit URI(std::string Path) { CopyFrom(Path); }
228 URI() : Port(0) {}
229};
230
231 struct SubstVar
232{
233 const char *Subst;
234 const std::string *Contents;
235};
236APT_PUBLIC std::string SubstVar(std::string Str,const struct SubstVar *Vars);
237APT_PUBLIC std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
238
239 struct RxChoiceList
240{
241 void *UserData;
242 const char *Str;
243 bool Hit;
244};
245APT_PUBLIC unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
246 const char **ListEnd);
247
248#endif
URI
Definition: strutl.h:208
RxChoiceList
Definition: strutl.h:240
SubstVar
Definition: strutl.h:232