From ed74106bcbfbb30c8cc62b665f24669dee4cf326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 26 Sep 2010 20:32:38 +0000 Subject: [PATCH] * Added more comments to the code. * Cleanup ParseStates in case the input didn't have all closing tags. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38824 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/mediaplayer/interface/SubtitleBitmap.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/apps/mediaplayer/interface/SubtitleBitmap.cpp b/src/apps/mediaplayer/interface/SubtitleBitmap.cpp index c77c5bc4f2..a5959a1caf 100644 --- a/src/apps/mediaplayer/interface/SubtitleBitmap.cpp +++ b/src/apps/mediaplayer/interface/SubtitleBitmap.cpp @@ -163,9 +163,11 @@ find_next_tag(const BString& string, int32& tagPos, int32& tagLength, tagPos = string.Length(); tagLength = 0; + // Find the next tag closest from the current position + // This way of doing it allows broken input with overlapping tags even. BString tag; for (int32 i = 0; i < kTagCount; i++) { - int32 nextTag = string.FindFirst(kTags[i], current); + int32 nextTag = string.IFindFirst(kTags[i], current); if (nextTag >= current && nextTag < tagPos) { tagPos = nextTag; tag = kTags[i]; @@ -173,6 +175,7 @@ find_next_tag(const BString& string, int32& tagPos, int32& tagLength, } if (tag != "") { + // Tag found, ParseState will change. tagLength = tag.Length(); if (tag == "") { state = new ParseState(state); @@ -197,7 +200,7 @@ find_next_tag(const BString& string, int32& tagPos, int32& tagLength, } } else if (tag == "" || tag == "" || tag == "" || tag == "") { - // Pop state + // Closing tag, pop state if (state->previous != NULL) { ParseState* oldState = state; state = state->previous; @@ -245,6 +248,7 @@ parse_text(const BString& string, BTextView* textView, const BFont& font, int32 tagLength; bool stateChanged = find_next_tag(string, nextPos, tagLength, state); if (nextPos > pos) { + // Insert text between last and next tags BString subString; string.CopyInto(subString, pos, nextPos - pos); textView->Insert(subString.String()); @@ -253,6 +257,13 @@ parse_text(const BString& string, BTextView* textView, const BFont& font, if (stateChanged) apply_state(textView, state, font, changeColor); } + + // Cleanup states in case the input text had non-matching tags. + while (state->previous != NULL) { + ParseState* oldState = state->previous; + state = state->previous; + delete oldState; + } }