void WindowCheckKeyterms::add_to_renderings(const ustring & rendering, bool wholeword)
// Adds "rendering" to renderings. If it contains any capitals, the 
// casesensitive is set too.
{
  ustring keyterm;
  keyterms_get_term(keyword_id, keyterm);
  GtkTreeIter iter;
  gtk_tree_store_append(treestore_renderings, &iter, NULL);
  bool casesensitive = rendering != rendering.casefold();
  gtk_tree_store_set(treestore_renderings, &iter, 0, wholeword, 1, casesensitive, 2, rendering.c_str(), 3, 1, -1);
  save_renderings();
}
bool WindowCheckKeyterms::find_renderings (const ustring& text, const vector <ustring>& renderings, const vector <bool>& wholewords, const vector <bool>& casesensitives, vector <size_t> * startpositions, vector <size_t> * lengths)
// Finds renderings in the text.
// text: Text to be looked into.
// renderings: Renderings to look for.
// wholewords / casesensitives: Attributes of the renderings.
// startpositions: If non-NULL, will be filled with the positions that each rendering starts at.
// lengths: If non-NULL, will be filled with the lengths of the renderings found.
// Returns whether one or more renderings were found in the verse.
{
  if (startpositions)
    startpositions->clear();
  if (lengths)
    lengths->clear();

  GtkTextBuffer * textbuffer = gtk_text_buffer_new (NULL);
  gtk_text_buffer_set_text (textbuffer, text.c_str(), -1);
  GtkTextIter startiter;
  gtk_text_buffer_get_start_iter(textbuffer, &startiter);

  bool found = false;

  for (unsigned int i2 = 0; i2 < renderings.size(); i2++) {

    ustring rendering = renderings[i2];
    bool wholeword = wholewords[i2];
    bool casesensitive = casesensitives[i2];

    ustring mytext;
    ustring myrendering;
    if (casesensitive) {
      mytext = text;
      myrendering = rendering;
    } else {
      mytext = text.casefold();
      myrendering = rendering.casefold();
    }
    size_t position = mytext.find(myrendering);
    while (position != string::npos) {
      bool temporally_approved = true;
      GtkTextIter approvedstart = startiter;
      GtkTextIter approvedend;
      gtk_text_iter_forward_chars(&approvedstart, position);
      approvedend = approvedstart;
      gtk_text_iter_forward_chars(&approvedend, rendering.length());
      if (wholeword) {
        if (!gtk_text_iter_starts_word(&approvedstart))
          temporally_approved = false;
        if (!gtk_text_iter_ends_word(&approvedend))
          temporally_approved = false;
      }
      if (temporally_approved) {
        found = true;
        if (startpositions)
          startpositions->push_back (position);
        if (lengths)
          lengths->push_back (rendering.length());
      }
      position = mytext.find(myrendering, ++position);
    }
  }

  g_object_unref (textbuffer);  
  
  return found;
}
Exemple #3
0
bool searchwords_find_fast (const ustring& text, 
                            const vector <ustring>& searchwords,
							const vector <bool>& wholewords,
							const vector <bool>& casesensitives, 
                            vector <size_t>& startpositions,
							vector <size_t>& lengths)
// Finds occurrences of searchwords in the text.
// text: Text to be looked through.
// searchwords: Search words to look for.
// wholewords / casesensitives: Attributes of the searchwords.
// startpositions: If non-NULL, will be filled with the positions that each searchword starts at.
// lengths: If non-NULL, will be filled with the lengths of the searchwords found.
// Returns whether one or more searchwords were found in the text.
{
  // Clear output containers.
  startpositions.clear();
  lengths.clear();

  // A textbuffer makes searching text easier in this case.
  GtkTextBuffer * textbuffer = gtk_text_buffer_new (NULL);
  gtk_text_buffer_set_text (textbuffer, text.c_str(), -1);
  GtkTextIter startiter;
  gtk_text_buffer_get_start_iter(textbuffer, &startiter);

  bool found = false;

  // Go through all words to look for.
  for (unsigned int i2 = 0; i2 < searchwords.size(); i2++) {

    // Define this search word and its parameters.
    ustring searchword = searchwords[i2];
    bool wholeword = wholewords[i2];
    bool casesensitive = casesensitives[i2];

    // Handle case sensitivity.
    ustring mytext;
    ustring mysearchword;
    if (casesensitive) {
      mytext = text;
      mysearchword = searchword;
    } else {
      mytext = text.casefold();
      mysearchword = searchword.casefold();
    }
    // Find all occurrences of the word.
    size_t position = mytext.find(mysearchword);
    while (position != string::npos) {
      bool temporally_approved = true;
      GtkTextIter approvedstart = startiter;
      GtkTextIter approvedend;
      gtk_text_iter_forward_chars(&approvedstart, position);
      approvedend = approvedstart;
      gtk_text_iter_forward_chars(&approvedend, searchword.length());
      if (wholeword) {
        if (!gtk_text_iter_starts_word(&approvedstart))
          temporally_approved = false;
        if (!gtk_text_iter_ends_word(&approvedend))
          temporally_approved = false;
      }
      if (temporally_approved) {
        found = true;
        startpositions.push_back (position);
        lengths.push_back (searchword.length());
      }
      position = mytext.find(mysearchword, ++position);
    }
  }
 
  // Free textbuffer used.
  g_object_unref (textbuffer);  

  if (found) {
    // Sort the output.
    quick_sort (startpositions, lengths, 0, (unsigned int)startpositions.size());
    // Overlapping items need to be combined to avoid crashes.
    xml_combine_overlaps (startpositions, lengths);
  }

  // Return true if anything was found.  
  return found;
}