in_tag function now looks for a positive number of tag occurrences.

In particular: in "<tag><tag></tag>x</tag>" the x is now in_tag.
This fixes the rest of #20.

Also added is_in_tag function that returns a bool instead of the tag position.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1456 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2010-07-21 22:42:46 +00:00
parent 4c3acef3fa
commit f7ae4b5a7b
5 changed files with 38 additions and 14 deletions
+26 -6
View File
@@ -148,12 +148,32 @@ size_t last_start_tag_before(const String& str, const String& tag, size_t start)
}
size_t in_tag(const String& str, const String& tag, size_t start, size_t end) {
if (start > end) swap(start, end);
size_t pos = last_start_tag_before(str, tag, start);
if (pos == String::npos) return String::npos; // no tag found before start
size_t posE = match_close_tag(str, pos);
if (posE < end) return String::npos; // the tag ends before end
return pos;
size_t last_start = String::npos;
size_t size = str.size();
int taglevel = 0;
end = min(end,size);
for (size_t pos = 0 ; pos < end ; ) {
Char c = str.GetChar(pos);
if (c == _('<')) {
if (is_substr(str, pos + 1, tag.c_str()+1)) {
if (pos < start) last_start = pos;
++taglevel;
} else if (pos + 2 < size && str.GetChar(pos+1) == _('/') && is_substr(str, pos + 2, tag.c_str()+1)) {
--taglevel; // close tag
}
pos = skip_tag(str,pos);
} else {
pos++;
}
if (pos >= start && taglevel < 1) {
// not inside tag anymore
return String::npos;
}
}
return taglevel < 1 ? String::npos : last_start;
}
bool is_in_tag(const String& str, const String& tag, size_t start, size_t end) {
return in_tag(str,tag,start,end) != String::npos;
}
+5
View File
@@ -73,8 +73,13 @@ size_t last_start_tag_before(const String& str, const String& tag, size_t start)
/// Is the given range entirely contained in a given tagged block?
/** If so: return the start position of that tag, otherwise returns String::npos
* A tagged block is everything between <tag>...</tag>
* Note: this function looks for a positive number of tags, so in for example
* <tag><tag></tag>x</tag>
* the x is in_tag
*/
size_t in_tag(const String& str, const String& tag, size_t start, size_t end);
/// Boolean returning version of the above
bool is_in_tag(const String& str, const String& tag, size_t start, size_t end);
/// Return the tag at the given position (without the <>)
String tag_at(const String& str, size_t pos);