forked from aymara/lima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtstring.cpp
141 lines (121 loc) · 3.81 KB
/
tstring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2002-2013 CEA LIST
This file is part of LIMA.
LIMA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LIMA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with LIMA. If not, see <http://www.gnu.org/licenses/>
*/
/******************************************************************************
*
* File : tstring.cpp
* Author : Besancon Romaric ([email protected])
* Created on : Thu Nov 21 2002
* Copyright : (c) 2002 by CEA
* Version : $Id: tstring.cpp 2611 2005-10-20 20:21:45Z gael $
*
******************************************************************************/
#include "tstring.h"
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
namespace Lima {
namespace LinguisticProcessing {
namespace Automaton {
//***************************************************************************
// functions to deal with escape characters in a string
//***************************************************************************
void getlineLimaString(std::istream& in, LimaString& s) {
// first get a string and convert it to wstring
std::string tmp = Lima::Common::Misc::readLine(in);
s=Common::Misc::utf8stdstring2limastring(tmp);
}
//***************************************************************************
// functions to deal with escape characters in a string
//***************************************************************************
// tests if the character in indicated position is an effective escape
// character
bool isEscapeCharacter(const LimaString& s, const int i) {
if (i>s.size() || s[i] != CHAR_ESCAPE) {
return false;
}
else if (i != 0) {
// have to check the number of escape characters before this one :
// the escape character is effective is the number of escape characters
// before it is 0 or an even number
int j(i);
uint64_t numberOfEscapeBefore(0);
while (j!=0) {
j--;
if (s[j] == CHAR_ESCAPE) {
numberOfEscapeBefore++;
}
else {
break;
}
}
if (numberOfEscapeBefore%2 == 0) { return true; }
else { return false; }
}
else {
return true;
}
}
int findSpecialCharacter(const LimaString& s, const LimaChar c,
const int posBegin,
const int posEnd) {
int i(posBegin);
do {
i=s.indexOf(c,i);
if (posEnd>=0 && i>posEnd) {
return -1; // found but out of the limits
}
if ( i == -1 || i==0 ) {
return i;
}
if (! isEscapeCharacter(s,i-1)) {
return i;
}
i++;
} while (i!=-1);
return i;
}
int rfindSpecialCharacter(const LimaString& s, const LimaChar c,
const int posBegin,
const int posEnd) {
int i(posBegin);
do {
i=s.lastIndexOf(c,i);
if (posEnd>=0 && i<posEnd) {
return -1; // found but out of the limits
}
if ( i == -1 || i==posEnd ) {
return i;
}
if (! isEscapeCharacter(s,i-1)) {
return i;
}
i--;
} while (i>0);
return i;
}
void removeEscapeCharacters(LimaString& s) {
for (LimaString::iterator i(s.begin()); i!= s.end(); i++) {
if (*i == CHAR_ESCAPE) {
s.remove(*i);
// no changing i cause next character to be skipped,
// but that's what we want : don't remove second '\' in '\'
}
}
}
} // end namespace
} // end namespace
} // end namespace