Skip to content

Commit 0913912

Browse files
committed
Fix in_tag and is_tag calls, fixes #47
1 parent a0f077e commit 0913912

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

src/data/action/value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ unique_ptr<TextValueAction> typing_action(const TextValueP& value, size_t start_
203203
TextToggleReminderAction::TextToggleReminderAction(const TextValueP& value, size_t pos_in)
204204
: ValueAction(value)
205205
{
206-
pos = in_tag(value->value(), _("<kw-"), pos_in, pos_in);
206+
pos = in_tag(value->value(), _("<kw"), pos_in, pos_in);
207207
if (pos == String::npos) {
208208
throw InternalError(_("TextToggleReminderAction: not in <kw- tag"));
209209
}

src/data/keyword.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,9 @@ String expand_keywords(const String& tagged_str, vector<KeywordMatch> const& mat
531531
} else {
532532
bool is_close = (it+1) != end && *(it+1) == '/';
533533
if (is_close && !close || !is_close && !open) return;
534-
if (is_substr(it, end, "<atom")) {
534+
if (is_tag(it, end, "<atom")) {
535535
atom++;
536-
} else if (is_substr(it, end, "</atom")) {
536+
} else if (is_tag(it, end, "</atom")) {
537537
atom++;
538538
}
539539
// keep tag in output
@@ -602,7 +602,7 @@ String::const_iterator keyword_match_detail(String::const_iterator it, String::c
602602
// The even captures are parameter values, the odd ones are the plain text in between
603603
// submatch 0 is the whole match
604604
assert(match.size() - 1 == 1 + 2 * keyword.parameters.size());
605-
for (int sub = 1; sub < match.size(); ++sub) {
605+
for (int sub = 1; sub < (int)match.size(); ++sub) {
606606
// The matched part, indices in untagged string. We only need the length
607607
size_t part_len_untagged = match.length(sub);
608608
// Translate back to tagged position

src/gui/value/text.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ void TextValueEditor::onLoseFocus() {
553553

554554
bool TextValueEditor::onContextMenu(wxMenu& m, wxContextMenuEvent& ev) {
555555
// in a keword? => "reminder text" option
556-
size_t kwpos = in_tag(value().value(), _("<kw-"), selection_start_i, selection_start_i);
556+
size_t kwpos = in_tag(value().value(), _("<kw"), selection_start_i, selection_start_i);
557557
if (kwpos != String::npos) {
558558
m.InsertSeparator(0);
559559
m.Insert(0, make_menu_item_tr(&m, ID_FORMAT_REMINDER, "reminder", "reminder_text", wxITEM_CHECK));

src/util/tagged_string.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ wxUniChar tag_char(wxUniChar c) {
2323
else return c;
2424
}
2525

26+
// Is a character the "end" of the tag name?
27+
// don't mistake <tag> as <t>, only <t>, <t-stuff> and <t:stuff> are considered <t>
28+
bool is_tag_end_char(Char c) {
29+
return c == '>' || c == '-' || c == ':' || c == ' ';
30+
}
31+
2632

2733
String untag(const String& str) {
2834
bool in_tag = false;
@@ -156,6 +162,14 @@ String fix_old_tags(const String& str) {
156162
return skip_all_tags(it, end, after_open, after_close);
157163
}
158164

165+
[[nodiscard]] bool is_tag(String::const_iterator it, String::const_iterator end, const char* tag) {
166+
for (; *tag; ++it, ++tag) {
167+
if (it == end || *it != *tag) return false;
168+
}
169+
if (it == end || !is_tag_end_char(*it)) return false;
170+
return true;
171+
}
172+
159173
/*
160174
// Does the string [it..end) contain the matching close tag for [tag..tag_end)?
161175
bool is_close_tag(String::const_iterator it, String::const_iterator end, String::const_iterator tag, String::const_iterator tag_end) {
@@ -253,11 +267,6 @@ String::const_iterator find_close_tag(String::const_iterator tag, String::const_
253267
return String::npos;
254268
}
255269

256-
// don't mistake <tag> as <t>, only <t>, <t-stuff> and <t:stuff> are considered <t>
257-
bool is_tag_end_char(Char c) {
258-
return c == '>' || c == '-' || c == ':' || c == ' ';
259-
}
260-
261270
bool is_tag(const String& str, size_t pos, const String& tag) {
262271
return is_substr(str, pos, tag) && pos+tag.size() < str.size() && is_tag_end_char(str[pos+tag.size()]);
263272
}

src/util/tagged_string.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ String anti_tag(const String& tag);
119119
// If not found, returns end
120120
[[nodiscard]] String::const_iterator find_close_tag(String::const_iterator it, String::const_iterator end);
121121

122+
/// Does a string contain a tag at the given location?
123+
/** Only matches if the tag in the text ends one of ">-: "
124+
* tag should be "<tag" or "</tag"
125+
*/
126+
[[nodiscard]] bool is_tag(String::const_iterator it, String::const_iterator end, const char* tag);
127+
122128
// Length of a string when not counting tags
123129
// For example: untagged_length("<b>abc</b>",_) = 3
124130
[[nodiscard]] size_t untagged_length(String::const_iterator it, String::const_iterator end);

0 commit comments

Comments
 (0)