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