apt 2.9.14 commandline package manager

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