| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #include "PHighlightParser.h" | ||
| 8 | |||
| 9 | ///Default constructor of PHighlightParser | ||
| 10 | 1 | PHighlightParser::PHighlightParser(){ | |
| 11 |
1/1✓ Branch 0 (4→5) taken 1 times.
|
1 | initialisationPHighlightParser(); |
| 12 | 1 | } | |
| 13 | |||
| 14 | ///Destructor of PHighlightParser | ||
| 15 | 1 | PHighlightParser::~PHighlightParser(){ | |
| 16 | 1 | clear(); | |
| 17 | 1 | } | |
| 18 | |||
| 19 | ///Add token in the highlighter | ||
| 20 | /** @param vecToken : vector of token to be treated with the same replace before and after | ||
| 21 | * @param replaceBeforePattern : PString to be placed before the found pattern | ||
| 22 | * @param replaceAfterPattern : PString to be placed after the found pattern | ||
| 23 | */ | ||
| 24 | 2 | void PHighlightParser::addToken(const PVecString & vecToken, const PString & replaceBeforePattern, const PString & replaceAfterPattern){ | |
| 25 |
3/6✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
✗ Branch 4 (4→5) not taken.
✓ Branch 5 (4→6) taken 2 times.
✗ Branch 6 (27→28) not taken.
✗ Branch 7 (27→29) not taken.
|
2 | PHighlightingAction* action = new PHighlightingAction; |
| 26 | 2 | action->type = PHighlightType::REPLACE; | |
| 27 |
1/1✓ Branch 0 (6→7) taken 2 times.
|
2 | action->replaceBeforePattern = replaceBeforePattern; |
| 28 |
1/1✓ Branch 0 (7→8) taken 2 times.
|
2 | action->replaceAfterPattern = replaceAfterPattern; |
| 29 |
1/1✓ Branch 0 (8→9) taken 2 times.
|
2 | p_vecPtrAction.push_back(action); |
| 30 |
2/2✓ Branch 0 (25→10) taken 4 times.
✓ Branch 1 (25→26) taken 2 times.
|
12 | for(PVecString::const_iterator it(vecToken.begin()); it != vecToken.end(); ++it){ |
| 31 |
2/2✓ Branch 0 (12→13) taken 4 times.
✓ Branch 2 (13→14) taken 4 times.
|
8 | p_replaceTree.addKeyValue(it->toVector(), action); |
| 32 | } | ||
| 33 | 2 | } | |
| 34 | |||
| 35 | ///Add a begin end pattern | ||
| 36 | /** @param beginPattern : begin pattern to find | ||
| 37 | * @param endPattern : end pattern of the sequence | ||
| 38 | * @param replaceBeforePattern : PString to be placed before the begin pattern | ||
| 39 | * @param replaceAfterPattern : PString to be placed after the end pattern | ||
| 40 | */ | ||
| 41 | 1 | void PHighlightParser::addToken(const PString & beginPattern, const PString & endPattern, const PString & replaceBeforePattern, const PString & replaceAfterPattern){ | |
| 42 |
3/6✓ Branch 0 (2→3) taken 1 times.
✓ Branch 2 (3→4) taken 1 times.
✗ Branch 4 (4→5) not taken.
✓ Branch 5 (4→6) taken 1 times.
✗ Branch 6 (14→15) not taken.
✗ Branch 7 (14→16) not taken.
|
1 | PHighlightingAction* action = new PHighlightingAction; |
| 43 | 1 | action->type = PHighlightType::GET_UNITL_WITHOUT_PATTERN; | |
| 44 |
1/1✓ Branch 0 (6→7) taken 1 times.
|
1 | action->replaceBeforePattern = replaceBeforePattern; |
| 45 |
1/1✓ Branch 0 (7→8) taken 1 times.
|
1 | action->replaceAfterPattern = replaceAfterPattern; |
| 46 |
1/1✓ Branch 0 (8→9) taken 1 times.
|
1 | action->endPattern = endPattern; |
| 47 |
1/1✓ Branch 0 (9→10) taken 1 times.
|
1 | p_vecPtrAction.push_back(action); |
| 48 |
2/2✓ Branch 0 (10→11) taken 1 times.
✓ Branch 2 (11→12) taken 1 times.
|
1 | p_replaceTree.addKeyValue(beginPattern.toVector(), action); |
| 49 | 1 | } | |
| 50 | |||
| 51 | ///Do the highlighting of the given PString | ||
| 52 | /** @param content : PString to be used | ||
| 53 | * @return PString with corresponding highlighting | ||
| 54 | */ | ||
| 55 | 1 | PString PHighlightParser::makeHighlighting(const PString & content){ | |
| 56 |
3/7✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→7) not taken.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→8) taken 1 times.
✗ Branch 4 (9→10) not taken.
✓ Branch 5 (9→12) taken 1 times.
✗ Branch 6 (10→11) not taken.
|
1 | if(content.size() == 0lu || p_replaceTree.getMapChild().size() == 0lu){return content;} |
| 57 |
1/1✓ Branch 0 (12→13) taken 1 times.
|
1 | PFileParser parser; |
| 58 |
1/1✓ Branch 0 (13→14) taken 1 times.
|
1 | parser.setFileContent(content); |
| 59 |
1/1✓ Branch 0 (14→15) taken 1 times.
|
1 | PReplaceHightlightTree::iterator it(p_replaceTree.begin()); |
| 60 | 1 | bool isPreviousMatchingString(false); | |
| 61 |
3/3✓ Branch 0 (15→16) taken 1 times.
✓ Branch 2 (16→17) taken 1 times.
✓ Branch 4 (17→18) taken 1 times.
|
1 | PString out(""), pattern(""), replacePattern(""); |
| 62 | 1 | PHighlightingAction* action = NULL; | |
| 63 | 1 | size_t beginTest(0lu), nbMatch(0lu), indexLastMatchReplace(0lu); | |
| 64 |
3/3✓ Branch 0 (54→55) taken 61 times.
✓ Branch 2 (55→19) taken 60 times.
✓ Branch 3 (55→56) taken 1 times.
|
61 | while(!parser.isEndOfFile()){ |
| 65 |
1/1✓ Branch 0 (19→20) taken 60 times.
|
60 | char currentChar = parser.getCurrentCh(); |
| 66 | // std::cerr << "PString::replace : content["<<i<<"] = '"<<currentChar<<"'" << std::endl; | ||
| 67 |
3/3✓ Branch 0 (20→21) taken 60 times.
✓ Branch 2 (21→22) taken 23 times.
✓ Branch 3 (21→35) taken 37 times.
|
60 | if(it.next(currentChar)){ //We found a matching character |
| 68 |
2/2✓ Branch 0 (22→23) taken 9 times.
✓ Branch 1 (22→25) taken 14 times.
|
23 | if(nbMatch == 0lu){ //This is the first matched character for this sequence |
| 69 |
1/1✓ Branch 0 (23→24) taken 9 times.
|
9 | beginTest = parser.getCurrentCharIdx(); //So we remember the begining position |
| 70 | } | ||
| 71 |
1/1✓ Branch 0 (25→26) taken 23 times.
|
23 | pattern += currentChar; |
| 72 | 23 | ++nbMatch; //Let's remember how much characters were OK | |
| 73 |
2/2✓ Branch 0 (28→29) taken 5 times.
✓ Branch 1 (28→34) taken 18 times.
|
23 | if(it->hasValue()){ //If the current node tree has a value |
| 74 | 5 | action = it->getValue(); | |
| 75 |
1/1✓ Branch 0 (31→32) taken 5 times.
|
5 | replacePattern = pattern; //We have to remember the current pattern because it will change if we have other partial matches |
| 76 | 5 | isPreviousMatchingString = true; //And we say that we have a value | |
| 77 |
1/1✓ Branch 0 (32→33) taken 5 times.
|
5 | indexLastMatchReplace = parser.getCurrentCharIdx(); |
| 78 | } | ||
| 79 |
1/1✓ Branch 0 (34→53) taken 23 times.
|
23 | parser.incrementCurrentChar(); |
| 80 | }else{ | ||
| 81 |
2/2✓ Branch 0 (35→36) taken 28 times.
✓ Branch 1 (35→42) taken 9 times.
|
37 | if(nbMatch == 0lu){ |
| 82 |
2/2✓ Branch 0 (36→37) taken 28 times.
✓ Branch 2 (37→38) taken 28 times.
|
28 | it = p_replaceTree.begin(); //Let's check again from the begining of the tree |
| 83 | 28 | isPreviousMatchingString = false; | |
| 84 |
1/1✓ Branch 0 (39→40) taken 28 times.
|
28 | pattern = ""; |
| 85 |
1/1✓ Branch 0 (40→41) taken 28 times.
|
28 | out += currentChar; |
| 86 |
1/1✓ Branch 0 (41→53) taken 28 times.
|
28 | parser.incrementCurrentChar(); |
| 87 | }else{ | ||
| 88 |
2/2✓ Branch 0 (42→43) taken 5 times.
✓ Branch 1 (42→44) taken 4 times.
|
9 | if(isPreviousMatchingString){ //If we had something to replace |
| 89 |
1/1✓ Branch 0 (43→48) taken 5 times.
|
5 | doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it |
| 90 | }else{ | ||
| 91 |
2/2✓ Branch 0 (44→45) taken 4 times.
✓ Branch 2 (45→46) taken 4 times.
|
4 | out += parser.getChar(beginTest); //no sequence matches |
| 92 |
1/1✓ Branch 0 (46→47) taken 4 times.
|
4 | parser.setCurrentCharIdx(beginTest); //so we start again one character later (we will have the ++i of the for loop for that) |
| 93 |
1/1✓ Branch 0 (47→48) taken 4 times.
|
4 | parser.incrementCurrentChar(); |
| 94 | } | ||
| 95 | 9 | isPreviousMatchingString = false; | |
| 96 |
1/1✓ Branch 0 (48→49) taken 9 times.
|
9 | pattern = ""; |
| 97 | 9 | action = NULL; | |
| 98 | 9 | nbMatch = 0lu; | |
| 99 |
2/2✓ Branch 0 (49→50) taken 9 times.
✓ Branch 2 (50→51) taken 9 times.
|
9 | it = p_replaceTree.begin(); //Let's check again from the begining of the tree |
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 |
1/2✗ Branch 0 (56→57) not taken.
✓ Branch 1 (56→58) taken 1 times.
|
1 | if(isPreviousMatchingString){ //If we had something to replace |
| 104 | ✗ | doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it | |
| 105 |
1/2✗ Branch 0 (58→59) not taken.
✓ Branch 1 (58→65) taken 1 times.
|
1 | }else if(nbMatch != 0lu){ |
| 106 | ✗ | for(size_t i(beginTest); i < parser.getNbTotalChar(); ++i){ | |
| 107 | ✗ | out += parser.getChar(i); | |
| 108 | |||
| 109 | } | ||
| 110 | } | ||
| 111 | //We are potentially at the end of the source, so no more test | ||
| 112 |
1/1✓ Branch 0 (65→66) taken 1 times.
|
1 | return out; |
| 113 | 1 | } | |
| 114 | |||
| 115 | ///Clear all PHighlightingAction | ||
| 116 | 1 | void PHighlightParser::clear(){ | |
| 117 |
2/2✓ Branch 0 (18→3) taken 3 times.
✓ Branch 1 (18→19) taken 1 times.
|
8 | for(std::vector<PHighlightingAction*>::iterator it(p_vecPtrAction.begin()); it != p_vecPtrAction.end(); ++it){ |
| 118 |
1/2✓ Branch 0 (5→6) taken 3 times.
✗ Branch 1 (5→8) not taken.
|
3 | delete *it; |
| 119 | } | ||
| 120 | 1 | } | |
| 121 | |||
| 122 | ///Initialisation function of the class PHighlightParser | ||
| 123 | 1 | void PHighlightParser::initialisationPHighlightParser(){ | |
| 124 | |||
| 125 | 1 | } | |
| 126 | |||
| 127 | ///Do the replace action | ||
| 128 | /** @param[out] out : PString to be modified | ||
| 129 | * @param parser : PFileParser to be used to parse the current content | ||
| 130 | * @param action : PHighlightingAction to be performed | ||
| 131 | * @param replacePattern : current replace pattern (to put replace before and after PString around it) | ||
| 132 | * @param indexLastMatchReplace : index of the last replace ends | ||
| 133 | */ | ||
| 134 | 5 | void PHighlightParser::doReplaceAction(PString & out, PFileParser & parser, PHighlightingAction* action, const PString & replacePattern, size_t indexLastMatchReplace){ | |
| 135 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
|
5 | if(action == NULL){return;} |
| 136 |
2/2✓ Branch 0 (4→5) taken 4 times.
✓ Branch 1 (4→12) taken 1 times.
|
5 | if(action->type == PHighlightType::REPLACE){ |
| 137 |
3/3✓ Branch 0 (5→6) taken 4 times.
✓ Branch 2 (6→7) taken 4 times.
✓ Branch 4 (7→8) taken 4 times.
|
4 | out += action->replaceBeforePattern + replacePattern + action->replaceAfterPattern; //Let's replace it |
| 138 | 4 | parser.setCurrentCharIdx(indexLastMatchReplace); | |
| 139 | 4 | parser.incrementCurrentChar(); | |
| 140 |
1/2✓ Branch 0 (12→13) taken 1 times.
✗ Branch 1 (12→25) not taken.
|
1 | }else if(action->type == PHighlightType::GET_UNITL_WITHOUT_PATTERN){ |
| 141 |
6/6✓ Branch 0 (13→14) taken 1 times.
✓ Branch 2 (14→15) taken 1 times.
✓ Branch 4 (15→16) taken 1 times.
✓ Branch 6 (16→17) taken 1 times.
✓ Branch 8 (17→18) taken 1 times.
✓ Branch 10 (18→19) taken 1 times.
|
1 | out += action->replaceBeforePattern + replacePattern + parser.getUntilKeyWithoutPatern(action->endPattern) + action->endPattern + action->replaceAfterPattern; //Let's replace it |
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 |