2012-02-13 01:45:05 +00:00
/**
* Copyright ( c ) since 2009 Simon Steele - http : //untidy.net/
* Based on the work of Simon Steele for Programmer ' s Notepad 2 ( http : //untidy.net)
* Converted from boost : : xpressive to boost : : regex and performance improvements
* ( principally caching the compiled regex ) , and support for UTF8 encoded text
* ( c ) 2012 Dave Brotherstone - Changes for boost : : regex
2019-05-04 18:14:48 +00:00
* ( c ) 2013 Francois - R . Boyer @ PolyMtl . ca - Empty match modes and best match backward search
* ( c ) 2019 Don Ho - Adapt for upgrading Scitilla ( to version 4.1 .4 ) and boost ( to version 1.70 )
2012-02-13 01:45:05 +00:00
*
*/
2019-05-04 18:14:48 +00:00
2012-02-13 01:45:05 +00:00
# include <stdlib.h>
2013-08-28 00:44:27 +00:00
# include <vector>
2021-02-28 00:45:09 +00:00
# include <memory>
2020-09-06 07:24:24 +00:00
# include <string_view>
# include <stdexcept>
2022-01-04 23:07:50 +00:00
# include <optional>
2019-05-04 18:14:48 +00:00
# include "Scintilla.h"
2022-01-04 23:07:50 +00:00
# include "ScintillaTypes.h"
# include "ScintillaMessages.h"
# include "Debugging.h"
# include "Geometry.h"
2012-02-13 01:45:05 +00:00
# include "Platform.h"
2019-05-04 18:14:48 +00:00
# include "ILoader.h"
# include "ILexer.h"
# include "Position.h"
# include "UniqueString.h"
2012-02-13 01:45:05 +00:00
# include "SplitVector.h"
# include "Partitioning.h"
# include "RunStyles.h"
2019-05-04 18:14:48 +00:00
# include "ContractionState.h"
2012-02-13 01:45:05 +00:00
# include "CellBuffer.h"
# include "CharClassify.h"
# include "Decoration.h"
# include "ILexer.h"
2013-08-28 00:44:27 +00:00
# include "CaseFolder.h"
2022-01-04 23:07:50 +00:00
# include "CharacterCategoryMap.h"
2012-02-13 01:45:05 +00:00
# include "Document.h"
# include "UniConversion.h"
# include "UTF8DocumentIterator.h"
# include "AnsiDocumentIterator.h"
# include "BoostRegexSearch.h"
# include <boost/regex.hpp>
2021-02-28 00:45:09 +00:00
# include <boost/throw_exception.hpp>
2012-02-13 01:45:05 +00:00
# define CP_UTF8 65001
# define SC_CP_UTF8 65001
2022-01-04 23:07:50 +00:00
using namespace Scintilla : : Internal ;
2012-02-13 01:45:05 +00:00
using namespace boost ;
class BoostRegexSearch : public RegexSearchBase
{
public :
2013-01-25 00:46:29 +00:00
BoostRegexSearch ( ) : _substituted ( NULL ) { }
2012-02-13 01:45:05 +00:00
virtual ~ BoostRegexSearch ( )
{
2013-01-25 00:46:29 +00:00
delete [ ] _substituted ;
_substituted = NULL ;
2012-02-13 01:45:05 +00:00
}
2019-05-04 18:14:48 +00:00
virtual Sci : : Position FindText ( Document * doc , Sci : : Position minPos , Sci : : Position maxPos , const char * regex ,
2022-01-04 23:07:50 +00:00
bool caseSensitive , bool word , bool wordStart , Scintilla : : FindOption sciSearchFlags , Sci : : Position * lengthRet ) override ;
2012-02-13 01:45:05 +00:00
2022-01-04 23:07:50 +00:00
virtual const char * SubstituteByPosition ( Document * doc , const char * text , Sci : : Position * length ) override ;
2012-02-13 01:45:05 +00:00
private :
2013-01-25 00:46:29 +00:00
class SearchParameters ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
class Match : private DocWatcher {
public :
Match ( ) : _document ( NULL ) , _documentModified ( false ) , _position ( - 1 ) , _endPosition ( - 1 ) , _endPositionForContinuationCheck ( - 1 ) { }
~ Match ( ) { setDocument ( NULL ) ; }
2019-05-08 08:43:30 +00:00
Match ( Document * document , Sci : : Position position = - 1 , Sci : : Position endPosition = - 1 ) : _document ( NULL ) { set ( document , position , endPosition ) ; }
2013-01-25 00:46:29 +00:00
Match & operator = ( Match & m ) {
set ( m . _document , m . position ( ) , m . endPosition ( ) ) ;
return * this ;
}
2021-09-25 01:07:45 +00:00
Match & operator = ( void * /*nullptr*/ ) {
2013-01-25 00:46:29 +00:00
_position = - 1 ;
return * this ;
}
2019-05-08 08:43:30 +00:00
void set ( Document * document = NULL , Sci : : Position position = - 1 , Sci : : Position endPosition = - 1 ) {
2013-01-25 00:46:29 +00:00
setDocument ( document ) ;
_position = position ;
_endPositionForContinuationCheck = _endPosition = endPosition ;
_documentModified = false ;
}
2019-05-08 08:43:30 +00:00
bool isContinuationSearch ( Document * document , Sci : : Position startPosition , int direction ) {
2013-01-25 00:46:29 +00:00
if ( hasDocumentChanged ( document ) )
return false ;
if ( direction > 0 )
return startPosition = = _endPositionForContinuationCheck ;
else
return startPosition = = _position ;
}
bool isEmpty ( ) {
return _position = = _endPosition ;
}
2019-05-08 08:43:30 +00:00
Sci : : Position position ( ) {
2013-01-25 00:46:29 +00:00
return _position ;
}
2019-05-08 08:43:30 +00:00
Sci : : Position endPosition ( ) {
2013-01-25 00:46:29 +00:00
return _endPosition ;
}
2019-05-08 08:43:30 +00:00
Sci : : Position length ( ) {
2013-01-25 00:46:29 +00:00
return _endPosition - _position ;
}
int found ( ) {
return _position > = 0 ;
}
private :
bool hasDocumentChanged ( Document * currentDocument ) {
return currentDocument ! = _document | | _documentModified ;
}
void setDocument ( Document * newDocument ) {
if ( newDocument ! = _document )
{
if ( _document ! = NULL )
_document - > RemoveWatcher ( this , NULL ) ;
_document = newDocument ;
if ( _document ! = NULL )
_document - > AddWatcher ( this , NULL ) ;
}
}
// DocWatcher, so we can track modifications to know if we should consider a search to be a continuation of last search:
virtual void NotifyModified ( Document * modifiedDocument , DocModification mh , void * /*userData*/ )
{
if ( modifiedDocument = = _document )
{
2022-01-04 23:07:50 +00:00
if ( FlagSet ( mh . modificationType , ( Scintilla : : ModificationFlags : : Undo | Scintilla : : ModificationFlags : : Redo ) ) )
2013-01-25 00:46:29 +00:00
_documentModified = true ;
// Replacing last found text should not make isContinuationSearch return false.
2022-01-04 23:07:50 +00:00
else if ( FlagSet ( mh . modificationType , Scintilla : : ModificationFlags : : DeleteText ) )
2013-01-25 00:46:29 +00:00
{
if ( mh . position = = position ( ) & & mh . length = = length ( ) ) // Deleting what we last found.
_endPositionForContinuationCheck = _position ;
else _documentModified = true ;
}
2022-01-04 23:07:50 +00:00
else if ( FlagSet ( mh . modificationType , Scintilla : : ModificationFlags : : InsertText ) )
2013-01-25 00:46:29 +00:00
{
if ( mh . position = = position ( ) & & position ( ) = = _endPositionForContinuationCheck ) // Replace at last found position.
_endPositionForContinuationCheck + = mh . length ;
else _documentModified = true ;
}
}
}
2013-08-28 00:44:27 +00:00
2019-05-04 18:14:48 +00:00
virtual void NotifyDeleted ( Document * deletedDocument , void * /*userData*/ ) noexcept
2013-01-25 00:46:29 +00:00
{
if ( deletedDocument = = _document )
2013-08-28 00:44:27 +00:00
{
// We set the _document here, as we don't want to call the RemoveWatcher on this deleted document.
// Calling RemoveWatcher inside NotifyDeleted results in a crash, as NotifyDeleted is called whilst
// iterating on the watchers list (since Scintilla 3.x). Before 3.x, it was just a really bad idea.
_document = NULL ;
2013-01-25 00:46:29 +00:00
set ( NULL ) ;
2013-08-28 00:44:27 +00:00
}
2013-01-25 00:46:29 +00:00
}
virtual void NotifyModifyAttempt ( Document * /*document*/ , void * /*userData*/ ) { }
virtual void NotifySavePoint ( Document * /*document*/ , void * /*userData*/ , bool /*atSavePoint*/ ) { }
2019-05-08 08:43:30 +00:00
virtual void NotifyStyleNeeded ( Document * /*document*/ , void * /*userData*/ , Sci : : Position /*endPos*/ ) { }
2013-01-25 00:46:29 +00:00
virtual void NotifyLexerChanged ( Document * /*document*/ , void * /*userData*/ ) { }
2022-01-04 23:07:50 +00:00
virtual void NotifyErrorOccurred ( Document * /*document*/ , void * /*userData*/ , Scintilla : : Status /*status*/ ) { }
2013-01-25 00:46:29 +00:00
Document * _document ;
bool _documentModified ;
2019-05-08 08:43:30 +00:00
Sci : : Position _position , _endPosition ;
Sci : : Position _endPositionForContinuationCheck ;
2013-01-25 00:46:29 +00:00
} ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
class CharTPtr { // Automatically translatable from utf8 to wchar_t*, if required, with allocation and deallocation on destruction; char* is not deallocated.
public :
CharTPtr ( const char * ptr ) : _charPtr ( ptr ) , _wcharPtr ( NULL ) { }
~ CharTPtr ( ) {
delete [ ] _wcharPtr ;
}
operator const char * ( ) {
return _charPtr ;
}
operator const wchar_t * ( ) {
if ( _wcharPtr = = NULL )
_wcharPtr = utf8ToWchar ( _charPtr ) ;
return _wcharPtr ;
}
private :
const char * _charPtr ;
wchar_t * _wcharPtr ;
} ;
template < class CharT , class CharacterIterator >
class EncodingDependent {
public :
EncodingDependent ( ) : _lastCompileFlags ( - 1 ) { }
void compileRegex ( const char * regex , const int compileFlags ) ;
Match FindText ( SearchParameters & search ) ;
2019-05-08 08:43:30 +00:00
char * SubstituteByPosition ( const char * text , Sci : : Position * length ) ;
2013-01-25 00:46:29 +00:00
private :
Match FindTextForward ( SearchParameters & search ) ;
Match FindTextBackward ( SearchParameters & search ) ;
public :
typedef CharT Char ;
typedef basic_regex < CharT > Regex ;
typedef match_results < CharacterIterator > MatchResults ;
MatchResults _match ;
private :
Regex _regex ;
std : : string _lastRegexString ;
int _lastCompileFlags ;
} ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
class SearchParameters {
public :
2019-05-08 08:43:30 +00:00
Sci : : Position nextCharacter ( Sci : : Position position ) ;
bool isLineStart ( Sci : : Position position ) ;
bool isLineEnd ( Sci : : Position position ) ;
2013-01-25 00:46:29 +00:00
Document * _document ;
const char * _regexString ;
int _compileFlags ;
2019-05-08 08:43:30 +00:00
Sci : : Position _startPosition ;
Sci : : Position _endPosition ;
2013-01-25 00:46:29 +00:00
regex_constants : : match_flag_type _boostRegexFlags ;
int _direction ;
bool _is_allowed_empty ;
bool _is_allowed_empty_at_start_position ;
bool _skip_windows_line_end_as_one_character ;
} ;
static wchar_t * utf8ToWchar ( const char * utf8 ) ;
static char * wcharToUtf8 ( const wchar_t * w ) ;
static char * stringToCharPtr ( const std : : string & str ) ;
static char * stringToCharPtr ( const std : : wstring & str ) ;
EncodingDependent < char , AnsiDocumentIterator > _ansi ;
EncodingDependent < wchar_t , UTF8DocumentIterator > _utf8 ;
char * _substituted ;
Match _lastMatch ;
int _lastDirection ;
2012-02-13 01:45:05 +00:00
} ;
2022-01-04 23:07:50 +00:00
namespace Scintilla : : Internal
2012-02-13 01:45:05 +00:00
{
2015-06-11 15:41:07 +00:00
# ifdef SCI_OWNREGEX
2012-02-13 01:45:05 +00:00
RegexSearchBase * CreateRegexSearch ( CharClassify * /* charClassTable */ )
{
return new BoostRegexSearch ( ) ;
}
2015-06-11 15:41:07 +00:00
# endif
2012-02-13 01:45:05 +00:00
}
2021-02-23 18:04:12 +00:00
std : : string g_exceptionMessage ;
2012-02-13 01:45:05 +00:00
/**
* Find text in document , supporting both forward and backward
2013-01-25 00:46:29 +00:00
* searches ( just pass startPosition > endPosition to do a backward search ) .
2012-02-13 01:45:05 +00:00
*/
2019-05-04 18:14:48 +00:00
Sci : : Position BoostRegexSearch : : FindText ( Document * doc , Sci : : Position startPosition , Sci : : Position endPosition , const char * regexString ,
2022-01-04 23:07:50 +00:00
bool caseSensitive , bool /*word*/ , bool /*wordStart*/ , Scintilla : : FindOption sciSearchFlags , Sci : : Position * lengthRet )
2012-02-13 01:45:05 +00:00
{
2021-02-23 18:04:12 +00:00
g_exceptionMessage . clear ( ) ;
2013-01-25 00:46:29 +00:00
try {
2022-01-04 23:07:50 +00:00
SearchParameters search { } ;
2013-01-25 00:46:29 +00:00
search . _document = doc ;
if ( startPosition > endPosition
2021-09-25 01:07:45 +00:00
| | ( startPosition = = endPosition & & _lastDirection < 0 ) ) // If we search in an empty region, suppose the direction is the same as last search (this is only important to verify if there can be an empty match in that empty region).
2013-01-25 00:46:29 +00:00
{
search . _startPosition = endPosition ;
search . _endPosition = startPosition ;
search . _direction = - 1 ;
}
else
{
search . _startPosition = startPosition ;
search . _endPosition = endPosition ;
search . _direction = 1 ;
}
_lastDirection = search . _direction ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
// Range endpoints should not be inside DBCS characters, but just in case, move them.
search . _startPosition = doc - > MovePositionOutsideChar ( search . _startPosition , 1 , false ) ;
search . _endPosition = doc - > MovePositionOutsideChar ( search . _endPosition , 1 , false ) ;
const bool isUtf8 = ( doc - > CodePage ( ) = = SC_CP_UTF8 ) ;
search . _compileFlags =
regex_constants : : ECMAScript
| ( caseSensitive ? 0 : regex_constants : : icase ) ;
search . _regexString = regexString ;
search . _boostRegexFlags =
2022-01-04 23:07:50 +00:00
( ( static_cast < int > ( sciSearchFlags ) & SCFIND_REGEXP_DOTMATCHESNL ) ? regex_constants : : match_default : regex_constants : : match_not_dot_newline ) ;
2013-01-25 00:46:29 +00:00
2022-01-04 23:07:50 +00:00
const int empty_match_style = static_cast < int > ( sciSearchFlags ) & SCFIND_REGEXP_EMPTYMATCH_MASK ;
const int allow_empty_at_start = static_cast < int > ( sciSearchFlags ) & SCFIND_REGEXP_EMPTYMATCH_ALLOWATSTART ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
search . _is_allowed_empty = empty_match_style ! = SCFIND_REGEXP_EMPTYMATCH_NONE ;
search . _is_allowed_empty_at_start_position = search . _is_allowed_empty & &
( allow_empty_at_start
| | ! _lastMatch . isContinuationSearch ( doc , startPosition , search . _direction )
2021-09-25 01:07:45 +00:00
| | ( empty_match_style = = SCFIND_REGEXP_EMPTYMATCH_ALL & & ! _lastMatch . isEmpty ( ) ) // If last match is empty and this is a continuation, then we would have same empty match at start position, if it was allowed.
2013-01-25 00:46:29 +00:00
) ;
2022-01-04 23:07:50 +00:00
search . _skip_windows_line_end_as_one_character = ( static_cast < int > ( sciSearchFlags ) & SCFIND_REGEXP_SKIPCRLFASONE ) ! = 0 ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
Match match =
isUtf8 ? _utf8 . FindText ( search )
: _ansi . FindText ( search ) ;
2021-03-27 16:58:54 +00:00
2013-01-25 00:46:29 +00:00
if ( match . found ( ) )
2012-02-13 01:45:05 +00:00
{
2013-01-25 00:46:29 +00:00
* lengthRet = match . length ( ) ;
_lastMatch = match ;
return match . position ( ) ;
}
else
{
_lastMatch = NULL ;
return - 1 ;
2012-02-13 01:45:05 +00:00
}
}
2013-01-25 00:46:29 +00:00
2021-02-23 18:04:12 +00:00
catch ( regex_error & ex )
2012-02-13 01:45:05 +00:00
{
// -1 is normally used for not found, -2 is used here for invalid regex
2021-02-23 18:04:12 +00:00
g_exceptionMessage = ex . what ( ) ;
2012-02-13 01:45:05 +00:00
return - 2 ;
}
2021-02-23 18:04:12 +00:00
catch ( boost : : wrapexcept < std : : runtime_error > & ex )
{
g_exceptionMessage = ex . what ( ) ;
return - 3 ;
}
catch ( . . . )
{
g_exceptionMessage = " Unexpected exception while searching " ;
return - 3 ;
}
2013-01-25 00:46:29 +00:00
}
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
template < class CharT , class CharacterIterator >
BoostRegexSearch : : Match BoostRegexSearch : : EncodingDependent < CharT , CharacterIterator > : : FindText ( SearchParameters & search )
{
compileRegex ( search . _regexString , search . _compileFlags ) ;
return ( search . _direction > 0 )
? FindTextForward ( search )
: FindTextBackward ( search ) ;
}
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
template < class CharT , class CharacterIterator >
BoostRegexSearch : : Match BoostRegexSearch : : EncodingDependent < CharT , CharacterIterator > : : FindTextForward ( SearchParameters & search )
{
CharacterIterator endIterator ( search . _document , search . _endPosition , search . _endPosition ) ;
2020-10-14 12:47:00 +00:00
CharacterIterator baseIterator ( search . _document , 0 , search . _endPosition ) ;
2019-05-08 08:43:30 +00:00
Sci : : Position next_search_from_position = search . _startPosition ;
2013-01-25 00:46:29 +00:00
bool found = false ;
bool match_is_valid = false ;
do {
const bool end_reached = next_search_from_position > search . _endPosition ;
2020-10-14 12:47:00 +00:00
found = ! end_reached & & boost : : regex_search ( CharacterIterator ( search . _document , next_search_from_position , search . _endPosition ) , endIterator , _match , _regex , search . _boostRegexFlags , baseIterator ) ;
2013-01-25 00:46:29 +00:00
if ( found ) {
2019-05-08 08:43:30 +00:00
const Sci : : Position position = _match [ 0 ] . first . pos ( ) ;
const Sci : : Position length = _match [ 0 ] . second . pos ( ) - position ;
2013-01-25 00:46:29 +00:00
const bool match_is_non_empty = length ! = 0 ;
const bool is_allowed_empty_here = search . _is_allowed_empty & & ( search . _is_allowed_empty_at_start_position | | position > search . _startPosition ) ;
match_is_valid = match_is_non_empty | | is_allowed_empty_here ;
if ( ! match_is_valid )
next_search_from_position = search . nextCharacter ( position ) ;
}
} while ( found & & ! match_is_valid ) ;
if ( found )
return Match ( search . _document , _match [ 0 ] . first . pos ( ) , _match [ 0 ] . second . pos ( ) ) ;
else
return Match ( ) ;
}
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
template < class CharT , class CharacterIterator >
BoostRegexSearch : : Match BoostRegexSearch : : EncodingDependent < CharT , CharacterIterator > : : FindTextBackward ( SearchParameters & search )
{
// Change backward search into series of forward search. It is slow: search all backward becomes O(n^2) instead of O(n) (if search forward is O(n)).
//NOTE: Maybe we should cache results. Maybe we could reverse regex to do a real backward search, for simple regex.
search . _direction = 1 ;
const bool is_allowed_empty_at_end_position = search . _is_allowed_empty_at_start_position ;
search . _is_allowed_empty_at_start_position = search . _is_allowed_empty ;
2012-02-13 01:45:05 +00:00
2013-01-25 00:46:29 +00:00
MatchResults bestMatch ;
2019-05-08 08:43:30 +00:00
Sci : : Position bestPosition = - 1 ;
Sci : : Position bestEnd = - 1 ;
2013-01-25 00:46:29 +00:00
for ( ; ; ) {
Match matchRange = FindText ( search ) ;
if ( ! matchRange . found ( ) )
break ;
2019-05-08 08:43:30 +00:00
Sci : : Position position = matchRange . position ( ) ;
Sci : : Position endPosition = matchRange . endPosition ( ) ;
2013-01-25 00:46:29 +00:00
if ( endPosition > bestEnd & & ( endPosition < search . _endPosition | | position ! = endPosition | | is_allowed_empty_at_end_position ) ) // We are searching for the longest match which has the fathest end (but may not accept empty match at end position).
2012-02-13 01:45:05 +00:00
{
2013-01-25 00:46:29 +00:00
bestMatch = _match ;
bestPosition = position ;
bestEnd = endPosition ;
2012-02-13 01:45:05 +00:00
}
2013-01-25 00:46:29 +00:00
search . _startPosition = search . nextCharacter ( position ) ;
2012-02-13 01:45:05 +00:00
}
2013-01-25 00:46:29 +00:00
if ( bestPosition > = 0 )
return Match ( search . _document , bestPosition , bestEnd ) ;
2012-02-13 01:45:05 +00:00
else
2013-01-25 00:46:29 +00:00
return Match ( ) ;
}
template < class CharT , class CharacterIterator >
void BoostRegexSearch : : EncodingDependent < CharT , CharacterIterator > : : compileRegex ( const char * regex , const int compileFlags )
{
if ( _lastCompileFlags ! = compileFlags | | _lastRegexString ! = regex )
2012-02-13 01:45:05 +00:00
{
2013-01-25 00:46:29 +00:00
_regex = Regex ( CharTPtr ( regex ) , static_cast < regex_constants : : syntax_option_type > ( compileFlags ) ) ;
_lastRegexString = regex ;
_lastCompileFlags = compileFlags ;
2012-02-13 01:45:05 +00:00
}
}
2019-05-08 08:43:30 +00:00
Sci : : Position BoostRegexSearch : : SearchParameters : : nextCharacter ( Sci : : Position position )
2013-01-25 00:46:29 +00:00
{
if ( _skip_windows_line_end_as_one_character & & _document - > CharAt ( position ) = = ' \r ' & & _document - > CharAt ( position + 1 ) = = ' \n ' )
return position + 2 ;
else
return position + 1 ;
}
2019-05-08 08:43:30 +00:00
bool BoostRegexSearch : : SearchParameters : : isLineStart ( Sci : : Position position )
2013-01-25 00:46:29 +00:00
{
return ( position = = 0 )
| | _document - > CharAt ( position - 1 ) = = ' \n '
2021-09-25 01:07:45 +00:00
| | ( _document - > CharAt ( position - 1 ) = = ' \r ' & & _document - > CharAt ( position ) ! = ' \n ' ) ;
2013-01-25 00:46:29 +00:00
}
2019-05-08 08:43:30 +00:00
bool BoostRegexSearch : : SearchParameters : : isLineEnd ( Sci : : Position position )
2013-01-25 00:46:29 +00:00
{
return ( position = = _document - > Length ( ) )
| | _document - > CharAt ( position ) = = ' \r '
2021-09-25 01:07:45 +00:00
| | ( _document - > CharAt ( position ) = = ' \n ' & & ( position = = 0 | | _document - > CharAt ( position - 1 ) ! = ' \n ' ) ) ;
2013-01-25 00:46:29 +00:00
}
2012-02-13 01:45:05 +00:00
2019-05-08 08:43:30 +00:00
const char * BoostRegexSearch : : SubstituteByPosition ( Document * doc , const char * text , Sci : : Position * length ) {
2013-01-25 00:46:29 +00:00
delete [ ] _substituted ;
_substituted = ( doc - > CodePage ( ) = = SC_CP_UTF8 )
? _utf8 . SubstituteByPosition ( text , length )
: _ansi . SubstituteByPosition ( text , length ) ;
return _substituted ;
}
template < class CharT , class CharacterIterator >
2019-05-08 08:43:30 +00:00
char * BoostRegexSearch : : EncodingDependent < CharT , CharacterIterator > : : SubstituteByPosition ( const char * text , Sci : : Position * length ) {
2013-01-25 00:46:29 +00:00
char * substituted = stringToCharPtr ( _match . format ( ( const CharT * ) CharTPtr ( text ) , boost : : format_all ) ) ;
2015-06-11 15:41:07 +00:00
* length = static_cast < int > ( strlen ( substituted ) ) ;
2012-02-13 01:45:05 +00:00
return substituted ;
}
wchar_t * BoostRegexSearch : : utf8ToWchar ( const char * utf8 )
{
2015-06-11 15:41:07 +00:00
size_t utf8Size = strlen ( utf8 ) ;
2019-05-04 18:14:48 +00:00
std : : string s ( utf8 , utf8Size ) ;
size_t wcharSize = UTF16Length ( s ) ;
2012-02-13 01:45:05 +00:00
wchar_t * w = new wchar_t [ wcharSize + 1 ] ;
2019-05-04 18:14:48 +00:00
UTF16FromUTF8 ( s , w , wcharSize + 1 ) ;
2012-02-13 01:45:05 +00:00
w [ wcharSize ] = 0 ;
return w ;
}
2013-01-25 00:46:29 +00:00
char * BoostRegexSearch : : wcharToUtf8 ( const wchar_t * w )
2012-02-13 01:45:05 +00:00
{
2019-05-04 18:14:48 +00:00
//int wcharSize = static_cast<int>(wcslen(w));
std : : wstring ws ( w ) ;
2019-05-08 08:43:30 +00:00
size_t charSize = UTF8Length ( ws ) ;
2012-02-13 01:45:05 +00:00
char * c = new char [ charSize + 1 ] ;
2019-05-04 18:14:48 +00:00
UTF8FromUTF16 ( ws , c , charSize ) ;
2012-02-13 01:45:05 +00:00
c [ charSize ] = 0 ;
return c ;
2013-01-25 00:46:29 +00:00
}
char * BoostRegexSearch : : stringToCharPtr ( const std : : string & str )
{
char * charPtr = new char [ str . length ( ) + 1 ] ;
strcpy ( charPtr , str . c_str ( ) ) ;
return charPtr ;
}
char * BoostRegexSearch : : stringToCharPtr ( const std : : wstring & str )
{
return wcharToUtf8 ( str . c_str ( ) ) ;
2012-02-13 01:45:05 +00:00
}