added to convert: PatternReplacer

for replacing parameters in patterns e.g. Replace("param %0", "first param")




git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1163 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2019-01-07 03:29:34 +00:00
parent 6c4a76baad
commit e9df044f9e
9 changed files with 191 additions and 28 deletions

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2012, Tomasz Sowa * Copyright (c) 2012-2018, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -40,6 +40,7 @@
#include "inttostr.h" #include "inttostr.h"
#include "patternreplacer.h"
#include "strtoint.h" #include "strtoint.h"
#include "text.h" #include "text.h"

169
convert/patternreplacer.h Normal file
View File

@ -0,0 +1,169 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfile_picotools_convert_patternreplacer
#define headerfile_picotools_convert_patternreplacer
#include <string>
#include "textstream/textstream.h"
#include "strtoint.h"
namespace PT
{
template<typename CharType, typename StrType>
class PatternReplacerBase
{
public:
/*
* output_string can be the same string as input pattern
*/
template<typename ... Types>
void ReplaceToString(const StrType & pattern, StrType & output_string, Types ... types)
{
ReplaceGeneric(pattern, output_string, types...);
}
template<typename ... Types>
StrType Replace(const StrType & pattern, Types ... types)
{
StrType output_string;
ReplaceGeneric(pattern, output_string, types...);
return output_string;
}
private:
std::vector<StrType> params;
TextStreamBase<CharType, 256, 4096> buffer;
StrType temp_str;
template<typename ... Types>
void ReplaceGeneric(const StrType & pattern, StrType & output_string, Types ... types)
{
params.clear();
AddParams(types...);
ReplacePattern(pattern);
buffer.to_string(output_string);
params.clear();
buffer.clear();
temp_str.clear();
}
void AddParams()
{
}
template<typename Type, typename ... Types>
void AddParams(const Type & type, Types ... types)
{
buffer.clear();
buffer << type;
buffer.to_string(temp_str);
params.push_back(temp_str);
temp_str.clear();
AddParams(types...);
}
void ReplacePattern(const StrType & pattern)
{
buffer.clear();
for(size_t i=0 ; i<pattern.size() ; )
{
bool pattern_changed = false;
if( pattern[i] == '%' )
{
if( i + 1 < pattern.size() )
{
if( pattern[i+1] == '%' )
{
buffer << '%';
i += 2;
pattern_changed = true;
}
else
{
const CharType * index_start_str = &pattern[i+1];
const CharType * after_str;
bool was_overflow;
unsigned long index = Toul(index_start_str, 10, &after_str, &was_overflow, false);
if( !was_overflow && after_str > index_start_str && (size_t)index < params.size() )
{
i = i + 1 + (after_str - index_start_str);
buffer << params[index];
pattern_changed = true;
}
}
}
}
if( !pattern_changed )
{
buffer << pattern[i];
i += 1;
}
}
}
};
typedef PatternReplacerBase<char, std::string> PatternReplacer;
typedef PatternReplacerBase<wchar_t, std::wstring> WPatternReplacer;
}
#endif

View File

@ -1,4 +1,3 @@
# DO NOT DELETE # DO NOT DELETE
date.o: date.h ../convert/convert.h ../convert/inttostr.h date.o: date.h ../convert/inttostr.h
date.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h

View File

@ -40,7 +40,8 @@
#include <ctime> #include <ctime>
#include <string> #include <string>
#include "convert/convert.h" #include "convert/inttostr.h"
namespace PT namespace PT

View File

@ -1,12 +1,9 @@
# DO NOT DELETE # DO NOT DELETE
filelog.o: filelog.h ../textstream/textstream.h ../space/space.h filelog.o: filelog.h ../textstream/textstream.h ../space/space.h
filelog.o: ../textstream/types.h ../date/date.h ../convert/convert.h filelog.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h
filelog.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h filelog.o: ../membuffer/membuffer.h ../textstream/types.h ../utf8/utf8.h
filelog.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h
filelog.o: ../utf8/utf8.h
log.o: log.h ../textstream/textstream.h ../space/space.h log.o: log.h ../textstream/textstream.h ../space/space.h
log.o: ../textstream/types.h ../date/date.h ../convert/convert.h log.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h
log.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h log.o: ../membuffer/membuffer.h ../textstream/types.h filelog.h
log.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h log.o: ../utf8/utf8.h
log.o: filelog.h ../utf8/utf8.h

View File

@ -2,6 +2,5 @@
mainspaceparser.o: mainspaceparser.h ../space/space.h ../textstream/types.h mainspaceparser.o: mainspaceparser.h ../space/space.h ../textstream/types.h
mainspaceparser.o: ../utf8/utf8.h ../textstream/textstream.h ../date/date.h mainspaceparser.o: ../utf8/utf8.h ../textstream/textstream.h ../date/date.h
mainspaceparser.o: ../convert/convert.h ../convert/inttostr.h mainspaceparser.o: ../convert/inttostr.h ../membuffer/membuffer.h
mainspaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h mainspaceparser.o: ../textstream/types.h
mainspaceparser.o: ../membuffer/membuffer.h ../textstream/types.h

View File

@ -2,18 +2,16 @@
jsontospaceparser.o: jsontospaceparser.h space.h ../textstream/types.h jsontospaceparser.o: jsontospaceparser.h space.h ../textstream/types.h
jsontospaceparser.o: ../utf8/utf8.h ../textstream/textstream.h jsontospaceparser.o: ../utf8/utf8.h ../textstream/textstream.h
jsontospaceparser.o: ../space/space.h ../date/date.h ../convert/convert.h jsontospaceparser.o: ../space/space.h ../date/date.h ../convert/inttostr.h
jsontospaceparser.o: ../convert/inttostr.h ../convert/strtoint.h
jsontospaceparser.o: ../convert/text.h ../convert/misc.h
jsontospaceparser.o: ../membuffer/membuffer.h ../textstream/types.h jsontospaceparser.o: ../membuffer/membuffer.h ../textstream/types.h
space.o: space.h ../textstream/types.h ../utf8/utf8.h space.o: space.h ../textstream/types.h ../utf8/utf8.h
space.o: ../textstream/textstream.h ../space/space.h ../date/date.h space.o: ../textstream/textstream.h ../space/space.h ../date/date.h
space.o: ../convert/convert.h ../convert/inttostr.h ../convert/strtoint.h space.o: ../convert/inttostr.h ../membuffer/membuffer.h ../textstream/types.h
space.o: ../convert/text.h ../convert/misc.h ../membuffer/membuffer.h space.o: ../convert/convert.h ../convert/inttostr.h
space.o: ../textstream/types.h space.o: ../convert/patternreplacer.h ../convert/strtoint.h ../convert/text.h
space.o: ../convert/misc.h
spaceparser.o: spaceparser.h space.h ../textstream/types.h ../utf8/utf8.h spaceparser.o: spaceparser.h space.h ../textstream/types.h ../utf8/utf8.h
spaceparser.o: ../textstream/textstream.h ../space/space.h ../date/date.h spaceparser.o: ../textstream/textstream.h ../space/space.h ../date/date.h
spaceparser.o: ../convert/convert.h ../convert/inttostr.h spaceparser.o: ../convert/inttostr.h ../membuffer/membuffer.h
spaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h spaceparser.o: ../textstream/types.h
spaceparser.o: ../membuffer/membuffer.h ../textstream/types.h
spacetojson.o: spacetojson.h space.h ../textstream/types.h spacetojson.o: spacetojson.h space.h ../textstream/types.h

View File

@ -41,7 +41,7 @@
#include <string> #include <string>
#include "space/space.h" #include "space/space.h"
#include "date/date.h" #include "date/date.h"
#include "convert/convert.h" #include "convert/inttostr.h"
#include "membuffer/membuffer.h" #include "membuffer/membuffer.h"
#include "types.h" #include "types.h"

View File

@ -1,6 +1,5 @@
# DO NOT DELETE # DO NOT DELETE
utf8.o: utf8.h ../textstream/textstream.h ../space/space.h utf8.o: utf8.h ../textstream/textstream.h ../space/space.h
utf8.o: ../textstream/types.h ../date/date.h ../convert/convert.h utf8.o: ../textstream/types.h ../date/date.h ../convert/inttostr.h
utf8.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h utf8.o: ../membuffer/membuffer.h ../textstream/types.h
utf8.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h