build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / helper / strhelper.cxx
blob4a5446aabf60d93d19b489695d481d8ba64c222e
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>
21 #include "sal/alloca.h"
23 namespace {
25 inline bool isSpace( sal_Unicode cChar )
27 return
28 cChar == ' ' || cChar == '\t' ||
29 cChar == '\r' || cChar == '\n' ||
30 cChar == 0x0c || cChar == 0x0b;
33 inline bool isProtect( sal_Unicode cChar )
35 return cChar == '`' || cChar == '\'' || cChar == '"';
38 inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, bool bIncludeUntil = false )
42 if( *pFrom == '\\' )
44 pFrom++;
45 if( *pFrom )
47 *pTo = *pFrom;
48 pTo++;
51 else if( bIncludeUntil || ! isProtect( *pFrom ) )
53 *pTo = *pFrom;
54 pTo++;
56 pFrom++;
57 } while( *pFrom && *pFrom != cUntil );
58 // copy the terminating character unless zero or protector
59 if( ! isProtect( *pFrom ) || bIncludeUntil )
61 *pTo = *pFrom;
62 if( *pTo )
63 pTo++;
65 if( *pFrom )
66 pFrom++;
69 inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, bool bIncludeUntil = false )
73 if( *pFrom == '\\' )
75 pFrom++;
76 if( *pFrom )
78 *pTo = *pFrom;
79 pTo++;
82 else if( bIncludeUntil || ! isProtect( *pFrom ) )
84 *pTo = *pFrom;
85 pTo++;
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 OString(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( const OUString& rLine, bool bProtect )
256 sal_Int32 nLen = rLine.getLength();
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.getStr();
262 sal_Unicode *pLeap = pBuffer;
264 while( *pRun )
266 if( *pRun && isSpace( *pRun ) )
268 *pLeap = ' ';
269 pLeap++;
270 pRun++;
272 while( *pRun && isSpace( *pRun ) )
273 pRun++;
274 while( *pRun && ! isSpace( *pRun ) )
276 if( *pRun == '\\' )
278 // escapement
279 pRun++;
280 *pLeap = *pRun;
281 pLeap++;
282 if( *pRun )
283 pRun++;
285 else if( bProtect && *pRun == '`' )
286 CopyUntil( pLeap, pRun, '`', true );
287 else if( bProtect && *pRun == '\'' )
288 CopyUntil( pLeap, pRun, '\'', true );
289 else if( bProtect && *pRun == '"' )
290 CopyUntil( pLeap, pRun, '"', true );
291 else
293 *pLeap = *pRun;
294 ++pLeap;
295 ++pRun;
300 *pLeap = 0;
302 // there might be a space at beginning or end
303 if (pLeap > pBuffer)
305 pLeap--;
306 if( *pLeap == ' ' )
307 *pLeap = 0;
310 return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
313 OString WhitespaceToSpace(const OString& rLine)
315 sal_Int32 nLen = rLine.getLength();
316 if (!nLen)
317 return rLine;
319 char *pBuffer = static_cast<char*>(alloca( nLen + 1 ));
320 const char *pRun = rLine.getStr();
321 char *pLeap = pBuffer;
323 while( *pRun )
325 if( *pRun && isSpace( *pRun ) )
327 *pLeap = ' ';
328 pLeap++;
329 pRun++;
331 while( *pRun && isSpace( *pRun ) )
332 pRun++;
333 while( *pRun && ! isSpace( *pRun ) )
335 if( *pRun == '\\' )
337 // escapement
338 pRun++;
339 *pLeap = *pRun;
340 pLeap++;
341 if( *pRun )
342 pRun++;
344 else if( *pRun == '`' )
345 CopyUntil( pLeap, pRun, '`', true );
346 else if( *pRun == '\'' )
347 CopyUntil( pLeap, pRun, '\'', true );
348 else if( *pRun == '"' )
349 CopyUntil( pLeap, pRun, '"', true );
350 else
352 *pLeap = *pRun;
353 ++pLeap;
354 ++pRun;
359 *pLeap = 0;
361 // there might be a space at beginning or end
362 pLeap--;
363 if( *pLeap == ' ' )
364 *pLeap = 0;
366 return OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
369 } // namespace
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */