mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: stringOps removeComments and inplaceRemoveComments
- useful for manual handling of string with comments
This commit is contained in:
committed by
Andrew Heather
parent
603849383d
commit
13967565a6
@ -44,6 +44,20 @@ Description
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
void testCommentStripping(const std::string& s)
|
||||
{
|
||||
Info<< "input" << nl
|
||||
<< "========" << nl
|
||||
<< s << nl
|
||||
<< "========" << nl;
|
||||
|
||||
Info<< "output" << nl
|
||||
<< "========" << nl
|
||||
<< stringOps::removeComments(s) << nl
|
||||
<< "========" << nl << nl;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
@ -158,6 +172,35 @@ int main(int argc, char *argv[])
|
||||
Info<<"trimRight: " << stringOps::trimRight(test) << endl;
|
||||
Info<<"trim: " << stringOps::trim(test) << endl;
|
||||
|
||||
// Test comment stripping
|
||||
{
|
||||
Info<< nl << "Test comment stripping" << nl;
|
||||
testCommentStripping
|
||||
(
|
||||
"/String without comments/"
|
||||
);
|
||||
testCommentStripping
|
||||
(
|
||||
"Removed some/* C-comments */ / C comments"
|
||||
);
|
||||
testCommentStripping
|
||||
(
|
||||
"Removed some//C++ comments\n / C++ comments"
|
||||
);
|
||||
testCommentStripping
|
||||
(
|
||||
"Partly degenerate C comment </*/ C-comment..."
|
||||
);
|
||||
testCommentStripping
|
||||
(
|
||||
"Truncated C comment </* C-comment..."
|
||||
);
|
||||
testCommentStripping
|
||||
(
|
||||
"Truncated C++ comment <// C++ comment..."
|
||||
);
|
||||
}
|
||||
|
||||
if (false)
|
||||
{
|
||||
Info<<"test move construct - string size:" << test.size() << nl;
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
@ -1104,6 +1104,90 @@ void Foam::stringOps::inplaceTrim(std::string& s)
|
||||
}
|
||||
|
||||
|
||||
Foam::string Foam::stringOps::removeComments(const string& original)
|
||||
{
|
||||
string s(original);
|
||||
inplaceRemoveComments(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
void Foam::stringOps::inplaceRemoveComments(std::string& s)
|
||||
{
|
||||
const auto len = s.length();
|
||||
|
||||
if (len < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::string::size_type n = 0;
|
||||
|
||||
for (std::string::size_type i = 0; i < len; ++i)
|
||||
{
|
||||
char c = s[i];
|
||||
|
||||
if (n != i)
|
||||
{
|
||||
s[n] = c;
|
||||
}
|
||||
++n;
|
||||
|
||||
// The start of a C/C++ comment?
|
||||
if (c == '/')
|
||||
{
|
||||
++i;
|
||||
|
||||
if (i == len)
|
||||
{
|
||||
// No further characters
|
||||
break;
|
||||
}
|
||||
|
||||
c = s[i];
|
||||
|
||||
if (c == '/')
|
||||
{
|
||||
// C++ comment - search for end-of-line
|
||||
--n;
|
||||
i = s.find('\n', ++i);
|
||||
|
||||
if (i == std::string::npos)
|
||||
{
|
||||
// Trucated - done
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (c == '*')
|
||||
{
|
||||
// C comment - search for '*/'
|
||||
--n;
|
||||
i = s.find("*/", ++i, 2);
|
||||
|
||||
if (i == std::string::npos)
|
||||
{
|
||||
// Trucated - done
|
||||
break;
|
||||
}
|
||||
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not a C/C++ comment
|
||||
if (n != i)
|
||||
{
|
||||
s[n] = c;
|
||||
}
|
||||
++n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.resize(n);
|
||||
}
|
||||
|
||||
|
||||
Foam::string Foam::stringOps::lower(const string& original)
|
||||
{
|
||||
string s(original);
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011-2012 OpenFOAM Foundation
|
||||
@ -351,6 +351,13 @@ namespace stringOps
|
||||
void inplaceTrim(std::string& s);
|
||||
|
||||
|
||||
//- Return string with C/C++ comments removed
|
||||
string removeComments(const string& original);
|
||||
|
||||
//- Remove C/C++ comments inplace
|
||||
void inplaceRemoveComments(std::string& s);
|
||||
|
||||
|
||||
//- Return string transformed with std::tolower on each character
|
||||
string lower(const string& original);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user