more script functions and text editor improvements

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@100 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-12-06 21:41:02 +00:00
parent 42bec59451
commit 86328aaeec
10 changed files with 227 additions and 47 deletions
+4
View File
@@ -33,6 +33,10 @@ class Defaultable {
assert(is_default);
value = new_value;
}
/// Assigning a value, don't change the defaultness
inline void assignDontChangeDefault(const T& new_value) {
value = new_value;
}
/// Get access to the value
inline const T& operator () () const { return value; }
+45
View File
@@ -82,6 +82,14 @@ String fix_old_tags(const String& str) {
// ----------------------------------------------------------------------------- : Finding tags
size_t tag_start(const String& str, size_t pos) {
size_t start = str.find_last_of(_('<'), pos);
if (start == String::npos) return String::npos;
size_t end = skip_tag(str, start);
if (end <= pos) return String::npos;
return start;
}
size_t skip_tag(const String& str, size_t start) {
if (start >= str.size()) return String::npos;
size_t end = str.find_first_of(_('>'), start);
@@ -152,6 +160,43 @@ String anti_tag(const String& tag) {
}
// ----------------------------------------------------------------------------- : Global operations
String remove_tag(const String& str, const String& tag) {
if (tag.size() < 1) return str;
String ctag = close_tag(tag);
return remove_tag_exact(remove_tag_exact(str, tag), ctag);
}
String remove_tag_exact(const String& str, const String& tag) {
String ret; ret.reserve(str.size());
size_t start = 0, pos = str.find(tag);
while (pos != String::npos) {
ret += str.substr(start, pos - start); // before
// next
start = skip_tag(str, pos);
if (start > str.size()) break;
pos = str.find(tag, start);
}
ret += str.substr(start);
return ret;
}
String remove_tag_contents(const String& str, const String& tag) {
String ret; ret.reserve(str.size());
size_t start = 0, pos = str.find(tag);
while (pos != String::npos) {
size_t end = match_close_tag(str, pos);
if (end == String::npos) return ret; // missing close tag
ret += str.substr(start, pos - start);
// next
start = skip_tag(str, end);
if (start > str.size()) break;
pos = str.find(tag, start);
}
ret += str.substr(start);
return ret;
}
// ----------------------------------------------------------------------------- : Updates
/// Return all open or close tags in the given range from a string
+13 -3
View File
@@ -42,6 +42,14 @@ String fix_old_tags(const String&);
// ----------------------------------------------------------------------------- : Finding tags
/// Returns the position of the tag the given position is in
/** Returns String::npos if pos is not in a tag.
* In a tag is:
* < t a g >
* n y y y y n
*/
size_t tag_start(const String& str, size_t pos);
/// Returns the position just beyond the tag starting at start
size_t skip_tag(const String& str, size_t start);
@@ -53,8 +61,10 @@ size_t match_close_tag(const String& str, size_t start);
/** If not found returns String::npos */
size_t last_start_tag_before(const String& str, const String& tag, size_t start);
/// Is the given range entirely contained in a given tag?
/** If so: return the start position of that tag, otherwise returns String::npos */
/// 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>
*/
size_t in_tag(const String& str, const String& tag, size_t start, size_t end);
/// Return the tag at the given position (without the <>)
@@ -78,7 +88,7 @@ String anti_tag(const String& tag);
*/
String remove_tag(const String& str, const String& tag);
/// Remove all instances of tags starting with tag
/// Remove all instances of tags starting with tag, but not its close tag
String remove_tag_exact(const String& str, const String& tag);
/// Remove all instances of a tag (including contents) from a string