bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / helper / strhelper.cxx
blob1153c6b520cf20d4d5088a3014a852fc8847bee5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <vcl/strhelper.hxx>
22 namespace {
24 bool isSpace( sal_Unicode cChar )
26 return
27 cChar == ' ' || cChar == '\t' ||
28 cChar == '\r' || cChar == '\n' ||
29 cChar == 0x0c || cChar == 0x0b;
32 bool isProtect( sal_Unicode cChar )
34 return cChar == '`' || cChar == '\'' || cChar == '"';
37 void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, bool bIncludeUntil = false )
41 if( *pFrom == '\\' )
43 pFrom++;
44 if( *pFrom )
46 *pTo = *pFrom;
47 pTo++;
50 else if( bIncludeUntil || ! isProtect( *pFrom ) )
52 *pTo = *pFrom;
53 pTo++;
55 pFrom++;
56 } while( *pFrom && *pFrom != cUntil );
57 // copy the terminating character unless zero or protector
58 if( ! isProtect( *pFrom ) || bIncludeUntil )
60 *pTo = *pFrom;
61 if( *pTo )
62 pTo++;
64 if( *pFrom )
65 pFrom++;
68 void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, bool bIncludeUntil = false )
72 if( *pFrom == '\\' )
74 pFrom++;
75 if( *pFrom )
77 *pTo = *pFrom;
78 pTo++;
81 else if( bIncludeUntil || ! isProtect( *pFrom ) )
83 *pTo = *pFrom;
84 pTo++;
86 pFrom++;
87 } while( *pFrom && *pFrom != cUntil );
88 // copy the terminating character unless zero or protector
89 if( ! isProtect( *pFrom ) || bIncludeUntil )
91 *pTo = *pFrom;
92 if( *pTo )
93 pTo++;
95 if( *pFrom )
96 pFrom++;
101 namespace psp {
103 OUString GetCommandLineToken( int nToken, const OUString& rLine )
105 sal_Int32 nLen = rLine.getLength();
106 if( ! nLen )
107 return OUString();
109 int nActualToken = 0;
110 sal_Unicode* pBuffer = static_cast<sal_Unicode*>(alloca( sizeof(sal_Unicode)*( nLen + 1 ) ));
111 const sal_Unicode* pRun = rLine.getStr();
112 sal_Unicode* pLeap = nullptr;
114 while( *pRun && nActualToken <= nToken )
116 while( *pRun && isSpace( *pRun ) )
117 pRun++;
118 pLeap = pBuffer;
119 while( *pRun && ! isSpace( *pRun ) )
121 if( *pRun == '\\' )
123 // escapement
124 pRun++;
125 *pLeap = *pRun;
126 pLeap++;
127 if( *pRun )
128 pRun++;
130 else if( *pRun == '`' )
131 CopyUntil( pLeap, pRun, '`' );
132 else if( *pRun == '\'' )
133 CopyUntil( pLeap, pRun, '\'' );
134 else if( *pRun == '"' )
135 CopyUntil( pLeap, pRun, '"' );
136 else
138 *pLeap = *pRun;
139 pLeap++;
140 pRun++;
143 if( nActualToken != nToken )
144 pBuffer[0] = 0;
145 nActualToken++;
148 *pLeap = 0;
150 return OUString(pBuffer);
153 OString GetCommandLineToken(int nToken, const OString& rLine)
155 sal_Int32 nLen = rLine.getLength();
156 if (!nLen)
157 return rLine;
159 int nActualToken = 0;
160 char* pBuffer = static_cast<char*>(alloca( nLen + 1 ));
161 const char* pRun = rLine.getStr();
162 char* pLeap = nullptr;
164 while( *pRun && nActualToken <= nToken )
166 while( *pRun && isSpace( *pRun ) )
167 pRun++;
168 pLeap = pBuffer;
169 while( *pRun && ! isSpace( *pRun ) )
171 if( *pRun == '\\' )
173 // escapement
174 pRun++;
175 *pLeap = *pRun;
176 pLeap++;
177 if( *pRun )
178 pRun++;
180 else if( *pRun == '`' )
181 CopyUntil( pLeap, pRun, '`' );
182 else if( *pRun == '\'' )
183 CopyUntil( pLeap, pRun, '\'' );
184 else if( *pRun == '"' )
185 CopyUntil( pLeap, pRun, '"' );
186 else
188 *pLeap = *pRun;
189 pLeap++;
190 pRun++;
193 if( nActualToken != nToken )
194 pBuffer[0] = 0;
195 nActualToken++;
198 *pLeap = 0;
200 return pBuffer;
203 int GetCommandLineTokenCount(const OUString& rLine)
205 if (rLine.isEmpty())
206 return 0;
208 int nTokenCount = 0;
209 const sal_Unicode *pRun = rLine.getStr();
211 while( *pRun )
213 while( *pRun && isSpace( *pRun ) )
214 pRun++;
215 if( ! *pRun )
216 break;
217 while( *pRun && ! isSpace( *pRun ) )
219 if( *pRun == '\\' )
221 // escapement
222 pRun++;
223 if( *pRun )
224 pRun++;
226 else if( *pRun == '`' )
228 do pRun++; while( *pRun && *pRun != '`' );
229 if( *pRun )
230 pRun++;
232 else if( *pRun == '\'' )
234 do pRun++; while( *pRun && *pRun != '\'' );
235 if( *pRun )
236 pRun++;
238 else if( *pRun == '"' )
240 do pRun++; while( *pRun && *pRun != '"' );
241 if( *pRun )
242 pRun++;
244 else
245 pRun++;
247 nTokenCount++;
250 return nTokenCount;
253 OUString WhitespaceToSpace( const OUString& rLine, bool bProtect )
255 sal_Int32 nLen = rLine.getLength();
256 if( ! nLen )
257 return OUString();
259 sal_Unicode *pBuffer = static_cast<sal_Unicode*>(alloca( sizeof(sal_Unicode)*(nLen + 1) ));
260 const sal_Unicode *pRun = rLine.getStr();
261 sal_Unicode *pLeap = pBuffer;
263 while( *pRun )
265 if( *pRun && isSpace( *pRun ) )
267 *pLeap = ' ';
268 pLeap++;
269 pRun++;
271 while( *pRun && isSpace( *pRun ) )
272 pRun++;
273 while( *pRun && ! isSpace( *pRun ) )
275 if( *pRun == '\\' )
277 // escapement
278 pRun++;
279 *pLeap = *pRun;
280 pLeap++;
281 if( *pRun )
282 pRun++;
284 else if( bProtect && *pRun == '`' )
285 CopyUntil( pLeap, pRun, '`', true );
286 else if( bProtect && *pRun == '\'' )
287 CopyUntil( pLeap, pRun, '\'', true );
288 else if( bProtect && *pRun == '"' )
289 CopyUntil( pLeap, pRun, '"', true );
290 else
292 *pLeap = *pRun;
293 ++pLeap;
294 ++pRun;
299 *pLeap = 0;
301 // there might be a space at beginning or end
302 if (pLeap > pBuffer)
304 pLeap--;
305 if( *pLeap == ' ' )
306 *pLeap = 0;
309 return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
312 OString WhitespaceToSpace(const OString& rLine)
314 sal_Int32 nLen = rLine.getLength();
315 if (!nLen)
316 return rLine;
318 char *pBuffer = static_cast<char*>(alloca( nLen + 1 ));
319 const char *pRun = rLine.getStr();
320 char *pLeap = pBuffer;
322 while( *pRun )
324 if( *pRun && isSpace( *pRun ) )
326 *pLeap = ' ';
327 pLeap++;
328 pRun++;
330 while( *pRun && isSpace( *pRun ) )
331 pRun++;
332 while( *pRun && ! isSpace( *pRun ) )
334 if( *pRun == '\\' )
336 // escapement
337 pRun++;
338 *pLeap = *pRun;
339 pLeap++;
340 if( *pRun )
341 pRun++;
343 else if( *pRun == '`' )
344 CopyUntil( pLeap, pRun, '`', true );
345 else if( *pRun == '\'' )
346 CopyUntil( pLeap, pRun, '\'', true );
347 else if( *pRun == '"' )
348 CopyUntil( pLeap, pRun, '"', true );
349 else
351 *pLeap = *pRun;
352 ++pLeap;
353 ++pRun;
358 *pLeap = 0;
360 // there might be a space at beginning or end
361 pLeap--;
362 if( *pLeap == ' ' )
363 *pLeap = 0;
365 return *pBuffer == ' ' ? pBuffer+1 : pBuffer;
368 } // namespace
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */