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