1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 bool isSpace( sal_Unicode cChar
)
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 )
50 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
56 } while( *pFrom
&& *pFrom
!= cUntil
);
57 // copy the terminating character unless zero or protector
58 if( ! isProtect( *pFrom
) || bIncludeUntil
)
68 void CopyUntil( sal_Unicode
*& pTo
, const sal_Unicode
*& pFrom
, sal_Unicode cUntil
, bool bIncludeUntil
= false )
81 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
88 } while( *pFrom
&& *pFrom
!= cUntil
);
89 // copy the terminating character unless zero or protector
90 if( ! isProtect( *pFrom
) || bIncludeUntil
)
104 OUString
GetCommandLineToken( int nToken
, const OUString
& rLine
)
106 sal_Int32 nLen
= rLine
.getLength();
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
) )
120 while( *pRun
&& ! isSpace( *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
, '"' );
144 if( nActualToken
!= nToken
)
151 return OUString(pBuffer
);
154 OString
GetCommandLineToken(int nToken
, const OString
& rLine
)
156 sal_Int32 nLen
= rLine
.getLength();
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
) )
170 while( *pRun
&& ! isSpace( *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
, '"' );
194 if( nActualToken
!= nToken
)
204 int GetCommandLineTokenCount(const OUString
& rLine
)
210 const sal_Unicode
*pRun
= rLine
.getStr();
214 while( *pRun
&& isSpace( *pRun
) )
218 while( *pRun
&& ! isSpace( *pRun
) )
227 else if( *pRun
== '`' )
229 do pRun
++; while( *pRun
&& *pRun
!= '`' );
233 else if( *pRun
== '\'' )
235 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
239 else if( *pRun
== '"' )
241 do pRun
++; while( *pRun
&& *pRun
!= '"' );
254 OUString
WhitespaceToSpace( std::u16string_view rLine
, bool bProtect
)
256 size_t nLen
= rLine
.size();
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
) )
273 while( pRun
!= pEnd
&& isSpace( *pRun
) )
275 while( pRun
!= pEnd
&& ! isSpace( *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 );
303 // there might be a space at beginning or end
311 return OUString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
314 OString
WhitespaceToSpace(std::string_view rLine
)
316 size_t nLen
= rLine
.size();
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
) )
333 while( pRun
!= pEnd
&& isSpace( *pRun
) )
335 while( pRun
!= pEnd
&& ! isSpace( *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 );
363 // there might be a space at beginning or end
364 assert(pLeap
> pBuffer
);
366 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
367 #pragma GCC diagnostic push
368 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
372 #if defined(__GNUC__) && (__GNUC__ == 12 || __GNUC__ == 13)
373 #pragma GCC diagnostic pop
375 return *pBuffer
== ' ' ? pBuffer
+1 : pBuffer
;
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */