bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / helper / strhelper.cxx
blobebe48d1200efc640294cd07352675729e12ae567
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 <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 if( *pFrom )
87 pFrom++;
88 } while( *pFrom && *pFrom != cUntil );
89 // copy the terminating character unless zero or protector
90 if( ! isProtect( *pFrom ) || bIncludeUntil )
92 *pTo = *pFrom;
93 if( *pTo )
94 pTo++;
96 if( *pFrom )
97 pFrom++;
102 namespace psp {
104 OUString GetCommandLineToken( int nToken, const OUString& rLine )
106 sal_Int32 nLen = rLine.getLength();
107 if( ! nLen )
108 return OUString();
110 int nActualToken = 0;
111 sal_Unicode* pBuffer = static_cast<sal_Unicode*>(alloca( sizeof(sal_Unicode)*( nLen + 1 ) ));
112 const sal_Unicode* pRun = rLine.getStr();
113 sal_Unicode* pLeap = nullptr;
115 while( *pRun && nActualToken <= nToken )
117 while( *pRun && isSpace( *pRun ) )
118 pRun++;
119 pLeap = pBuffer;
120 while( *pRun && ! isSpace( *pRun ) )
122 if( *pRun == '\\' )
124 // escapement
125 pRun++;
126 *pLeap = *pRun;
127 pLeap++;
128 if( *pRun )
129 pRun++;
131 else if( *pRun == '`' )
132 CopyUntil( pLeap, pRun, '`' );
133 else if( *pRun == '\'' )
134 CopyUntil( pLeap, pRun, '\'' );
135 else if( *pRun == '"' )
136 CopyUntil( pLeap, pRun, '"' );
137 else
139 *pLeap = *pRun;
140 pLeap++;
141 pRun++;
144 if( nActualToken != nToken )
145 pBuffer[0] = 0;
146 nActualToken++;
149 *pLeap = 0;
151 return OUString(pBuffer);
154 OString GetCommandLineToken(int nToken, const OString& rLine)
156 sal_Int32 nLen = rLine.getLength();
157 if (!nLen)
158 return rLine;
160 int nActualToken = 0;
161 char* pBuffer = static_cast<char*>(alloca( nLen + 1 ));
162 const char* pRun = rLine.getStr();
163 char* pLeap = nullptr;
165 while( *pRun && nActualToken <= nToken )
167 while( *pRun && isSpace( *pRun ) )
168 pRun++;
169 pLeap = pBuffer;
170 while( *pRun && ! isSpace( *pRun ) )
172 if( *pRun == '\\' )
174 // escapement
175 pRun++;
176 *pLeap = *pRun;
177 pLeap++;
178 if( *pRun )
179 pRun++;
181 else if( *pRun == '`' )
182 CopyUntil( pLeap, pRun, '`' );
183 else if( *pRun == '\'' )
184 CopyUntil( pLeap, pRun, '\'' );
185 else if( *pRun == '"' )
186 CopyUntil( pLeap, pRun, '"' );
187 else
189 *pLeap = *pRun;
190 pLeap++;
191 pRun++;
194 if( nActualToken != nToken )
195 pBuffer[0] = 0;
196 nActualToken++;
199 *pLeap = 0;
201 return pBuffer;
204 int GetCommandLineTokenCount(const OUString& rLine)
206 if (rLine.isEmpty())
207 return 0;
209 int nTokenCount = 0;
210 const sal_Unicode *pRun = rLine.getStr();
212 while( *pRun )
214 while( *pRun && isSpace( *pRun ) )
215 pRun++;
216 if( ! *pRun )
217 break;
218 while( *pRun && ! isSpace( *pRun ) )
220 if( *pRun == '\\' )
222 // escapement
223 pRun++;
224 if( *pRun )
225 pRun++;
227 else if( *pRun == '`' )
229 do pRun++; while( *pRun && *pRun != '`' );
230 if( *pRun )
231 pRun++;
233 else if( *pRun == '\'' )
235 do pRun++; while( *pRun && *pRun != '\'' );
236 if( *pRun )
237 pRun++;
239 else if( *pRun == '"' )
241 do pRun++; while( *pRun && *pRun != '"' );
242 if( *pRun )
243 pRun++;
245 else
246 pRun++;
248 nTokenCount++;
251 return nTokenCount;
254 OUString WhitespaceToSpace( std::u16string_view rLine, bool bProtect )
256 size_t nLen = rLine.size();
257 if( ! nLen )
258 return OUString();
260 sal_Unicode *pBuffer = static_cast<sal_Unicode*>(alloca( sizeof(sal_Unicode)*(nLen + 1) ));
261 const sal_Unicode *pRun = rLine.data();
262 const sal_Unicode * const pEnd = rLine.data() + rLine.size();
263 sal_Unicode *pLeap = pBuffer;
265 while( pRun != pEnd )
267 if( pRun != pEnd && isSpace( *pRun ) )
269 *pLeap = ' ';
270 pLeap++;
271 pRun++;
273 while( pRun != pEnd && isSpace( *pRun ) )
274 pRun++;
275 while( pRun != pEnd && ! isSpace( *pRun ) )
277 if( *pRun == '\\' )
279 // escapement
280 pRun++;
281 *pLeap = *pRun;
282 pLeap++;
283 if( pRun != pEnd )
284 pRun++;
286 else if( bProtect && *pRun == '`' )
287 CopyUntil( pLeap, pRun, '`', true );
288 else if( bProtect && *pRun == '\'' )
289 CopyUntil( pLeap, pRun, '\'', true );
290 else if( bProtect && *pRun == '"' )
291 CopyUntil( pLeap, pRun, '"', true );
292 else
294 *pLeap = *pRun;
295 ++pLeap;
296 ++pRun;
301 *pLeap = 0;
303 // there might be a space at beginning or end
304 if (pLeap > pBuffer)
306 pLeap--;
307 if( *pLeap == ' ' )
308 *pLeap = 0;
311 return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
314 OString WhitespaceToSpace(std::string_view rLine)
316 size_t nLen = rLine.size();
317 if (!nLen)
318 return OString();
320 char *pBuffer = static_cast<char*>(alloca( nLen + 1 ));
321 const char *pRun = rLine.data();
322 const char * const pEnd = rLine.data() + rLine.size();
323 char *pLeap = pBuffer;
325 while( pRun != pEnd )
327 if( pRun != pEnd && isSpace( *pRun ) )
329 *pLeap = ' ';
330 pLeap++;
331 pRun++;
333 while( pRun != pEnd && isSpace( *pRun ) )
334 pRun++;
335 while( pRun != pEnd && ! isSpace( *pRun ) )
337 if( *pRun == '\\' )
339 // escapement
340 pRun++;
341 *pLeap = *pRun;
342 pLeap++;
343 if( pRun != pEnd )
344 pRun++;
346 else if( *pRun == '`' )
347 CopyUntil( pLeap, pRun, '`', true );
348 else if( *pRun == '\'' )
349 CopyUntil( pLeap, pRun, '\'', true );
350 else if( *pRun == '"' )
351 CopyUntil( pLeap, pRun, '"', true );
352 else
354 *pLeap = *pRun;
355 ++pLeap;
356 ++pRun;
361 *pLeap = 0;
363 // there might be a space at beginning or end
364 assert(pLeap > pBuffer);
365 pLeap--;
366 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
367 #pragma GCC diagnostic push
368 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
369 #endif
370 if( *pLeap == ' ' )
371 *pLeap = 0;
372 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
373 #pragma GCC diagnostic pop
374 #endif
375 return *pBuffer == ' ' ? pBuffer+1 : pBuffer;
378 } // namespace
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */