[DEV] set back the use of the etk::RegExp and etk::Buffer instead of std::regex and std::string ==> fast edit big files

This commit is contained in:
2017-03-14 21:56:56 +01:00
parent c4f59d8734
commit a42436092b
28 changed files with 238 additions and 280 deletions

View File

@@ -145,20 +145,13 @@ bool appl::Buffer::loadFile(const std::string& _name) {
m_cursorPos = 0;
setHighlightType("");
m_nbLines = 0;
if (file.exist() == false) {
APPL_ERROR("File : '" << m_fileName << "' does not exist...");
return false;
if (m_data.dumpFrom(m_fileName) == true ) {
countNumberofLine();
tryFindHighlightType();
m_isModify = false;
return true;
}
if (file.fileOpenRead() == false) {
APPL_ERROR("File : '" << m_fileName << "' Fail to open in read mode");
return false;
}
m_data = file.fileReadAllString();
file.fileClose();
countNumberofLine();
tryFindHighlightType();
m_isModify = false;
return true;
return false;
}
void appl::Buffer::setFileName(const std::string& _name) {
@@ -175,16 +168,12 @@ void appl::Buffer::setFileName(const std::string& _name) {
}
bool appl::Buffer::storeFile() {
etk::FSNode file(m_fileName);
if (file.fileOpenWrite() == false) {
APPL_ERROR("File : '" << m_fileName << "' Fail to open in write mode");
return false;
if (m_data.dumpIn(m_fileName) == true) {
APPL_INFO("saving file : " << m_fileName);
setModification(false);
return true;
}
file.fileWriteAll(m_data);
file.fileClose();
APPL_INFO("saving file : " << m_fileName);
setModification(false);
return true;
return false;
}
void appl::Buffer::setModification(bool _status) {
@@ -642,7 +631,7 @@ bool appl::Buffer::write(const std::string& _data, const appl::Buffer::Iterator&
position = 0;
}
APPL_VERBOSE("write at pos: " << (int64_t)_pos << " ==> " << position << " data : " << _data);
m_data.insert((size_t)position, _data);
m_data.insert(position, (int8_t*)(_data.c_str()), _data.size());
if (m_cursorPos < 0) {
m_cursorPos = 0;
}
@@ -659,7 +648,7 @@ bool appl::Buffer::replace(const std::string& _data, const appl::Buffer::Iterato
if (position < 0){
position = 0;
}
m_data.replace(m_data.begin() + position, m_data.begin() + (int64_t)_posEnd, _data.begin(), _data.end());
m_data.replace(position, (int64_t)_posEnd-(int64_t)_pos, (int8_t*)(_data.c_str()), _data.size());
regenerateHighLightAt(position, (int64_t)_posEnd-(int64_t)_pos, _data.size());
m_selectMode = false;
moveCursor(position+_data.size());
@@ -674,7 +663,7 @@ void appl::Buffer::removeSelection() {
}
int64_t startPos = getStartSelectionPos();
int64_t endPos = getStopSelectionPos();
m_data.erase(startPos, endPos-startPos);
m_data.remove(startPos, endPos-startPos);
regenerateHighLightAt(startPos, endPos-startPos, 0);
m_selectMode = false;
moveCursor(startPos);

View File

@@ -8,6 +8,7 @@
#include <etk/types.hpp>
#include <etk/os/FSNode.hpp>
#include <ewol/ewol.hpp>
#include <etk/Buffer.hpp>
#include <ewol/object/Object.hpp>
#include <ewol/widget/Widget.hpp>
#include <ewol/compositing/Text.hpp>
@@ -346,9 +347,9 @@ namespace appl {
*/
void setModification(bool _status);
protected:
std::string m_data; //!< copy of the file buffer
etk::Buffer m_data; //!< copy of the file buffer
public:
std::string& getData() {
etk::Buffer& getData() {
return m_data;
};
protected:

View File

@@ -123,6 +123,7 @@ appl::Highlight::~Highlight() {
bool appl::Highlight::isCompatible(const std::string& _name) {
for (auto &it : m_listExtentions) {
APPL_DEBUG(" check : " << it << "=?=" << _name);
// TODO: Remove dependency with the std::regex ...
std::regex expression;
try {
expression.assign(it, std::regex_constants::optimize | std::regex_constants::ECMAScript);
@@ -135,7 +136,7 @@ bool appl::Highlight::isCompatible(const std::string& _name) {
if (resultMatch.size() <= 0) {
continue;
}
APPL_VERBOSE(" - begin=" << std::distance(_name.begin(), resultMatch[0].first) << " end=" << std::distance(_name.begin(), resultMatch[0].second));
APPL_DEBUG(" - begin=" << std::distance(_name.begin(), resultMatch[0].first) << " end=" << std::distance(_name.begin(), resultMatch[0].second));
if (resultMatch[0].first != _name.begin()) {
continue;
}
@@ -198,7 +199,7 @@ void appl::Highlight::parse(int64_t _start,
int64_t _stop,
std::vector<appl::HighlightInfo> & _metaData,
int64_t _addingPos,
std::string& _buffer) {
etk::Buffer& _buffer) {
if (0 > _addingPos) {
_addingPos = 0;
}
@@ -278,7 +279,7 @@ void appl::Highlight::parse(int64_t _start,
void appl::Highlight::parse2(int64_t _start,
int64_t _stop,
std::vector<appl::HighlightInfo>& _metaData,
std::string& _buffer) {
etk::Buffer& _buffer) {
HL2_DEBUG("Parse element 0 => " << m_listHighlightPass2.size() <<
" == > position search: (" << _start << "," << _stop << ")" );
int64_t elementStart = _start;
@@ -286,21 +287,20 @@ void appl::Highlight::parse2(int64_t _start,
appl::HighlightInfo resultat;
while (elementStart < elementStop) {
if (elementStart == 306) {
//elog::setLevel(elog::logLevelVerbose);
}
//HL2_DEBUG("Parse element in the buffer pos=" << elementStart << "," << _buffer.size() << ")" );
HL2_DEBUG("Parse element in the buffer pos=" << elementStart << "," << _buffer.size() << ")" );
//try to fond the HL in ALL of we have
for (int64_t jjj=0; jjj<int64_t(m_listHighlightPass2.size()); jjj++){
/*
HL2_DEBUG("Parse HL id=" << jjj << " position search: (" <<
elementStart << "," << elementStop << ") in='"
<< /*_buffer[elementStart]*/ std::string(_buffer.begin()+elementStart,_buffer.begin()+elementStop) << "' " << m_listHighlightPass2[jjj].getPaternString().first << " " << m_listHighlightPass1[jjj].getPaternString().second);
<< std::string(_buffer.begin()+elementStart,_buffer.begin()+elementStop) << "' " << m_listHighlightPass2[jjj].getPaternString().first << " " << m_listHighlightPass1[jjj].getPaternString().second);
*/
// Stop the search to the end (to get the end of the pattern)
bool ret = m_listHighlightPass2[jjj].find(elementStart, elementStop, resultat, _buffer);
if (ret == true) {
// find an element:
_metaData.push_back(resultat);
HL2_DEBUG("data='" << std::string(_buffer.begin()+elementStart,_buffer.begin()+resultat.stop) << "'");
//HL2_DEBUG("data='" << std::string(_buffer.begin()+elementStart,_buffer.begin()+resultat.stop) << "'");
elementStart = resultat.stop-1;
break;
}
@@ -317,8 +317,8 @@ void appl::Highlight::parse2(int64_t _start,
* @param[in] _buffer buffer where we need to search data
*/
void appl::Highlight::parseSubElement(const appl::HighlightInfo& _upper,
std::vector<appl::HighlightInfo> &_metaData,
std::string &_buffer) {
std::vector<appl::HighlightInfo>& _metaData,
etk::Buffer& _buffer) {
if (_upper.patern->getSubPatternName().size() == 0) {
return;
}

View File

@@ -22,6 +22,7 @@ namespace appl {
#include <etk/os/FSNode.hpp>
#include <appl/HighlightPattern.hpp>
#include <appl/GlyphPainting.hpp>
#include <etk/Buffer.hpp>
#include <exml/exml.hpp>
namespace appl {
@@ -53,19 +54,20 @@ namespace appl {
int64_t _stop,
std::vector<appl::HighlightInfo>& _metaData,
int64_t _addingPos,
std::string& _buffer);
etk::Buffer& _buffer);
void parse2(int64_t _start,
int64_t _stop,
std::vector<appl::HighlightInfo>& _metaData,
std::string& _buffer);
etk::Buffer& _buffer);
void parseSubElement(const appl::HighlightInfo& _upper,
std::vector<appl::HighlightInfo>& _metaData,
std::string &_buffer);
etk::Buffer& _buffer);
private:
std::string m_styleName; //!< curent style name (like "c++" or "c" or "script Bash")
std::vector<std::string> m_listExtentions; //!< List of possible extention for this high-light, like : ".c", ".cpp", ".h"
std::vector<HighlightPattern> m_listHighlightPass1; //!< List of ALL hightlight modules (pass 1 == > when we load and wride data on the buffer)
std::vector<HighlightPattern> m_listHighlightPass2; //!< List of ALL hightlight modules (pass 2 == > When we display the buffer( only the display area (100 lines)) )
// TODO : This is bad ==> the patern ar unordered ...
std::map<std::string, std::vector<HighlightPattern>> m_listHighlightNamed; //!< list of all sub partern to parse...
public: // herited function :
virtual bool updateContext() {

View File

@@ -84,8 +84,7 @@ std::string appl::highlightManager::getTypeFile(const std::string& _fileName) {
}
APPL_DEBUG(" check : " << it->getTypeName());
if (it->isCompatible(_fileName) == true) {
APPL_DEBUG("Find type for extention : " << _fileName
<< " type : " << it->getTypeName());
APPL_DEBUG("Find type for extention : " << _fileName << " type : " << it->getTypeName());
return it->getTypeName();
}
}

View File

@@ -43,16 +43,16 @@ void appl::HighlightPattern::setPatern(const std::string& _regExp, const std::st
m_hasParsingError = false;
if (_regExp != "") {
try {
m_regExp[0].assign(_regExp, std::regex_constants::optimize | std::regex_constants::ECMAScript);
} catch (std::regex_error e) {
m_regExp[0].compile(_regExp);
} catch (std::runtime_error e) {
m_hasParsingError = true;
APPL_ERROR("can not parse regex : '" << e.what() << "' for : " << _regExp);
}
}
if (_regExpStop != "") {
try {
m_regExp[1].assign(_regExpStop, std::regex_constants::optimize | std::regex_constants::ECMAScript);
} catch (std::regex_error e) {
m_regExp[1].compile(_regExpStop);
} catch (std::runtime_error e) {
m_hasParsingError = true;
APPL_ERROR("can not parse regex : '" << e.what() << "' for : " << _regExpStop);
}
@@ -138,72 +138,10 @@ void appl::HighlightPattern::parseRules(const exml::Element& _child, int32_t _le
}
}
static std::pair<int64_t,int64_t> findRegex(int32_t _start,
int32_t _stop,
std::regex& _regex,
const std::string& _buffer) {
std::smatch resultMatch;
std::regex_constants::match_flag_type flags = std::regex_constants::match_continuous; // check only the match at the first character.
//APPL_DEBUG("find data at : start=" << _start << " stop=" << _stop << " regex='" << m_regexValue << "'");
if ((int64_t)_stop <= (int64_t)_buffer.size()) {
char val = _buffer[_stop];
if ( val != '\n'
&& val != '\r') {
//after last char ==> not end of line ($ would not work))
flags |= std::regex_constants::match_not_eol;
}
/*
if (!( ('a' <= val && val <= 'z')
|| ('A' <= val && val <= 'Z')
|| ('0' <= val && val <= '9')
|| val == '_')) {
flags |= std::regex_constants::match_not_eow;
}
*/
}
if (_start>0) {
flags |= std::regex_constants::match_prev_avail;
}
if ( _stop < 0
|| size_t(_stop) > _buffer.size()) {
APPL_ERROR(" error in indexing for regex ... _stop=" << _stop << " >= _buffer.size()=" << _buffer.size());
return std::pair<int64_t,int64_t>(-1,0);
}
if ( _start < 0
|| size_t(_start) > _buffer.size()) {
APPL_ERROR(" error in indexing for regex ... _start=" << _start << " >= _buffer.size()=" << _buffer.size());
return std::pair<int64_t,int64_t>(-1,0);
}
if (_start > _stop) {
APPL_ERROR(" error in indexing for regex ... _start=" << _start << " > _stop=" << _stop);
return std::pair<int64_t,int64_t>(-1,0);
}
std::regex_search(_buffer.begin() + _start, _buffer.begin() + _stop, resultMatch, _regex, flags);
if (resultMatch.size() > 0) {
int64_t start = std::distance(_buffer.begin(), resultMatch[0].first);
int64_t stop = std::distance(_buffer.begin(), resultMatch[0].second);
//APPL_DEBUG("find data at : start=" << _resultat.start << " stop=" << _resultat.stop << " data='" <<std::string(_buffer, _resultat.start, _resultat.stop-_resultat.start) << "'" );
/*
if (true){
//TK_DEBUG("in line : '" << etk::to_string(_buffer) << "'");
APPL_DEBUG(" Find " << resultMatch.size() << " elements");
for (size_t iii=0; iii<resultMatch.size(); ++iii) {
int32_t posStart = std::distance(_buffer.begin(), resultMatch[iii].first);
int32_t posStop = std::distance(_buffer.begin(), resultMatch[iii].second);
APPL_DEBUG(" [" << iii << "] " << posStart << " to " << posStop);
}
}
*/
return std::pair<int64_t,int64_t>(start,stop);
}
return std::pair<int64_t,int64_t>(-1,0);
}
bool appl::HighlightPattern::find(int32_t _start,
int32_t _stop,
appl::HighlightInfo& _resultat,
const std::string& _buffer) {
const etk::Buffer& _buffer) {
//APPL_DEBUG(" try to find the element");
_resultat.start = -1;
_resultat.stop = -1;
@@ -216,12 +154,12 @@ bool appl::HighlightPattern::find(int32_t _start,
if (m_regexValue[0].size() == 0) {
return false;
}
std::pair<int64_t,int64_t> ret0 = findRegex(_start, _stop, m_regExp[0], _buffer);
if (ret0.first >= 0) {
_resultat.start = ret0.first;
_resultat.stop = ret0.second;
// when we have only one element:
if (m_regExp[0].processOneElement(_buffer, _start, _stop) == true) {
_resultat.start = m_regExp[0].start();
_resultat.stop = m_regExp[0].stop();
//APPL_DEBUG("find data at : start=" << _resultat.start << " stop=" << _resultat.stop << " data='" <<std::string(_buffer, _resultat.start, _resultat.stop-_resultat.start) << "'" );
// second step : Complex searching ...
//APPL_DEBUG("find data at : start=" << _resultat.start << " stop=" << _resultat.stop );
if (m_hasEndRegEx == true) {
// when no regex specify ==> get all the buffer ...
if (m_regexValue[1].size() == 0) {
@@ -230,9 +168,8 @@ bool appl::HighlightPattern::find(int32_t _start,
}
_start = _resultat.stop;
while (_start < _stop) {
std::pair<int64_t,int64_t> ret1 = findRegex(_start, _stop, m_regExp[1], _buffer);
if (ret1.first >= 0) {
_resultat.stop = ret1.second;
if (m_regExp[1].processOneElement(_buffer, _start, _stop) == true) {
_resultat.stop = m_regExp[1].stop();
return true;
}
_start++;

View File

@@ -11,6 +11,8 @@ class HighlightPattern;
#include <appl/GlyphPainting.hpp>
#include <vector>
#include <regex>
#include <etk/RegExp.hpp>
#include <etk/Buffer.hpp>
#include <exml/exml.hpp>
namespace appl {
@@ -44,7 +46,7 @@ namespace appl {
bool m_hasParsingError;
std::string m_regexValue[2];
bool m_hasEndRegEx;
std::regex m_regExp[2]; //!< Start of Regular expression
etk::RegExp<etk::Buffer> m_regExp[2]; //!< Start of Regular expression
public:
void setPatern(const std::string& _regExp, const std::string& _regExpStop="", bool _hasEndRegEx=false);
std::pair<std::string,std::string> getPaternString();
@@ -81,7 +83,7 @@ namespace appl {
bool find(int32_t _start,
int32_t _stop,
appl::HighlightInfo& _resultat,
const std::string& _buffer);
const etk::Buffer& _buffer);
void parseRules(const exml::Element& _child, int32_t _level);
};

View File

@@ -102,7 +102,7 @@ class MainApplication : public ewol::context::Application {
std::string tmpppp = _context.getCmd().get(iii);
if (tmpppp == "-t") {
ctagDetected = true;
} else if (true == ctagDetected) {
} else if (ctagDetected == true) {
etk::FSNode file(tmpppp);
std::string name = file.getName();
APPL_INFO("Load ctag file : \"" << name << "\"" );
@@ -117,8 +117,18 @@ class MainApplication : public ewol::context::Application {
std::string name = file.getName();
APPL_INFO("need load file : \"" << name << "\"" );
m_bufferManager->open(name);
} else if (file.getNodeType() == etk::typeNode_folder) {
std::vector<std::string> listOfFiles = file.folderGetSub(false, true, ".*");
for (auto &it: listOfFiles) {
etk::FSNode file2(it);
if (file2.getNodeType() == etk::typeNode_file) {
std::string name = file2.getName();
APPL_INFO("need load file : \"" << name << "\"" );
m_bufferManager->open(name);
}
}
}
}
}
}
APPL_INFO("==> START ... " PROJECT_NAME " (END)");