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