]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/strutl.h
Fix several typos
[apt.git] / apt-pkg / contrib / strutl.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: strutl.h,v 1.22 2003/02/02 22:20:27 jgg Exp $
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 source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
13
14 ##################################################################### */
15 /*}}}*/
16 #ifndef STRUTL_H
17 #define STRUTL_H
18
19
20 #include <limits>
21 #include <string>
22 #include <cstring>
23 #include <vector>
24 #include <iostream>
25 #ifdef APT_PKG_EXPOSE_STRING_VIEW
26 #include <apt-pkg/string_view.h>
27 #endif
28 #include <time.h>
29 #include <stddef.h>
30
31 #include "macros.h"
32
33 #ifndef APT_10_CLEANER_HEADERS
34 #include <stdlib.h>
35 #endif
36 #ifndef APT_8_CLEANER_HEADERS
37 using std::string;
38 using std::vector;
39 using std::ostream;
40 #endif
41
42 namespace APT {
43 namespace String {
44 std::string Strip(const std::string &s);
45 bool Endswith(const std::string &s, const std::string &ending);
46 bool Startswith(const std::string &s, const std::string &starting);
47 }
48 }
49
50
51 bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest);
52 char *_strstrip(char *String);
53 char *_strrstrip(char *String); // right strip only
54 char *_strtabexpand(char *String,size_t Len);
55 bool ParseQuoteWord(const char *&String,std::string &Res);
56 bool ParseCWord(const char *&String,std::string &Res);
57 std::string QuoteString(const std::string &Str,const char *Bad);
58 std::string DeQuoteString(const std::string &Str);
59 std::string DeQuoteString(std::string::const_iterator const &begin, std::string::const_iterator const &end);
60
61 // unescape (\0XX and \xXX) from a string
62 std::string DeEscapeString(const std::string &input);
63
64 std::string SizeToStr(double Bytes);
65 std::string TimeToStr(unsigned long Sec);
66 std::string Base64Encode(const std::string &Str);
67 std::string OutputInDepth(const unsigned long Depth, const char* Separator=" ");
68 std::string URItoFileName(const std::string &URI);
69 std::string TimeRFC1123(time_t Date);
70 bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
71 bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK;
72 APT_DEPRECATED_MSG("Use RFC1123StrToTime or FTPMDTMStrToTime as needed instead") bool StrToTime(const std::string &Val,time_t &Result);
73 std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0);
74 int StringToBool(const std::string &Text,int Default = -1);
75 bool ReadMessages(int Fd, std::vector<std::string> &List);
76 bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base = 0);
77 bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base = 0);
78 bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len);
79 bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len);
80 bool Hex2Num(const std::string &Str,unsigned char *Num,unsigned int Length);
81 #ifdef APT_PKG_EXPOSE_STRING_VIEW
82 APT_HIDDEN bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length);
83 #endif
84 // input changing string split
85 bool TokSplitString(char Tok,char *Input,char **List,
86 unsigned long ListMax);
87
88 // split a given string by a char
89 std::vector<std::string> VectorizeString(std::string const &haystack, char const &split) APT_PURE;
90
91 /* \brief Return a vector of strings from string "input" where "sep"
92 * is used as the delimiter string.
93 *
94 * \param input The input string.
95 *
96 * \param sep The separator to use.
97 *
98 * \param maxsplit (optional) The maximum amount of splitting that
99 * should be done .
100 *
101 * The optional "maxsplit" argument can be used to limit the splitting,
102 * if used the string is only split on maxsplit places and the last
103 * item in the vector contains the remainder string.
104 */
105 std::vector<std::string> StringSplit(std::string const &input,
106 std::string const &sep,
107 unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_CONST;
108
109 void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
110 void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
111 char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
112 bool CheckDomainList(const std::string &Host, const std::string &List);
113
114 /* Do some compat mumbo jumbo */
115 #define tolower_ascii tolower_ascii_inline
116 #define isspace_ascii isspace_ascii_inline
117
118 APT_CONST APT_HOT
119 static inline int tolower_ascii_inline(int const c)
120 {
121 return (c >= 'A' && c <= 'Z') ? c + 32 : c;
122 }
123 APT_CONST APT_HOT
124 static inline int isspace_ascii_inline(int const c)
125 {
126 // 9='\t',10='\n',11='\v',12='\f',13='\r',32=' '
127 return (c >= 9 && c <= 13) || c == ' ';
128 }
129
130 std::string StripEpoch(const std::string &VerStr);
131
132 #define APT_MKSTRCMP(name,func) \
133 inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \
134 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
135 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));} \
136 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());} \
137 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);}
138
139 #define APT_MKSTRCMP2(name,func) \
140 inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \
141 inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \
142 inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \
143 inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}
144
145 int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
146 int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd);
147
148 /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that
149 case the definition of string::const_iterator is not the same as
150 const char * and we need these extra functions */
151 #if __GNUC__ >= 3
152 int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
153 const char *B,const char *BEnd);
154 int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd,
155 std::string::const_iterator B,std::string::const_iterator BEnd);
156 int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
157 const char *B,const char *BEnd);
158 int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd,
159 std::string::const_iterator B,std::string::const_iterator BEnd);
160
161 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));}
162 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));}
163 #endif
164
165 APT_MKSTRCMP2(stringcmp,stringcmp)
166 APT_MKSTRCMP2(stringcasecmp,stringcasecmp)
167
168 // Return the length of a NULL-terminated string array
169 size_t APT_PURE strv_length(const char **str_array);
170
171
172 inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}
173
174 class URI
175 {
176 void CopyFrom(const std::string &From);
177
178 public:
179
180 std::string Access;
181 std::string User;
182 std::string Password;
183 std::string Host;
184 std::string Path;
185 unsigned int Port;
186
187 operator std::string();
188 inline void operator =(const std::string &From) {CopyFrom(From);}
189 inline bool empty() {return Access.empty();};
190 static std::string SiteOnly(const std::string &URI);
191 static std::string ArchiveOnly(const std::string &URI);
192 static std::string NoUserPassword(const std::string &URI);
193
194 URI(std::string Path) {CopyFrom(Path);}
195 URI() : Port(0) {}
196 };
197
198 struct SubstVar
199 {
200 const char *Subst;
201 const std::string *Contents;
202 };
203 std::string SubstVar(std::string Str,const struct SubstVar *Vars);
204 std::string SubstVar(const std::string &Str,const std::string &Subst,const std::string &Contents);
205
206 struct RxChoiceList
207 {
208 void *UserData;
209 const char *Str;
210 bool Hit;
211 };
212 unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
213 const char **ListEnd);
214
215 #endif