From f134edb09d71526f7eb46ea1b84328fcff8249f6 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 8 Jan 2009 17:10:54 +0000 Subject: [PATCH] added check_tagged function that verifies that a tagged string is correct, this helps with debugging. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1302 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/util/tagged_string.cpp | 32 ++++++++++++++++++++++++++++++++ src/util/tagged_string.hpp | 14 +++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/util/tagged_string.cpp b/src/util/tagged_string.cpp index 4932f810..e7ab57e5 100644 --- a/src/util/tagged_string.cpp +++ b/src/util/tagged_string.cpp @@ -571,6 +571,38 @@ String simplify_tagged_overlap(const String& str) { return ret; } +// ----------------------------------------------------------------------------- : Verification + +void check_tagged(const String& str) { + for (size_t i = 0 ; i < str.size() ; ) { + if (str.GetChar(i) == _('<')) { + size_t end = skip_tag(str,i); + if (end == String::npos) { + handle_warning(_("Invalid tagged string: missing '>'"),false); + } + for (size_t j = i + 1 ; j + 1 < end ; ++j) { + Char c = str.GetChar(j); + if (c == _(' ') || c == _('<')) { + handle_warning(_("Invalid character in tag"),false); + } + } + if (str.GetChar(i+1) == _('/')) { + // close tag, don't check + } else if (is_substr(str,i,_(" tags + } else { + size_t close = match_close_tag(str,i); + if (close == String::npos) { + handle_warning(_("Invalid tagged string: missing close tag for <") + tag_at(str,i) + _(">"),false); + } + } + i = end; + } else { + ++i; + } + } +} + // ----------------------------------------------------------------------------- : Other utilities String curly_quotes(String str, bool curl) { diff --git a/src/util/tagged_string.hpp b/src/util/tagged_string.hpp index e2513fb5..a5d4bfad 100644 --- a/src/util/tagged_string.hpp +++ b/src/util/tagged_string.hpp @@ -175,12 +175,20 @@ String tagged_substr_replace(const String& input, size_t start, size_t end, cons // ----------------------------------------------------------------------------- : Simplification -/// Verify that a string is correctly tagged, if it is not, change it so it is +/// Verify that a string is correctly tagged /** Ensures that: * - All tags end, i.e. for each '<' there is a matching '>' - * - There are no tags containing '<' + * - There are no tags containing '<' or whitespace + * - For each open tag there is a matching close tag. + * + * In case of an error, throws an exception. */ -String verify_tagged(const String& str); +void check_tagged(const String& str); +#ifdef _DEBUG + #define assert_tagged check_tagged +#else + #define assert_tagged(_) +#endif /// Simplify a tagged string /** - merges adjecent open/close tags: "" --> ""