Пример #1
0
static SearchCase
inspect_case (const char *p, const char *pend)
{
	gboolean is_upper = TRUE;
	gboolean is_capital = TRUE;
	gboolean has_letter = FALSE;
	gboolean expect_upper = TRUE;

	for (; p < pend; p = g_utf8_next_char (p)) {
		gunichar c = g_utf8_get_char (p);
		if (g_unichar_isalpha (c)) {
			has_letter = TRUE;
			if (!g_unichar_isupper (c)) {
				is_upper = FALSE;
			}

			if (expect_upper ? !g_unichar_isupper (c) : !g_unichar_islower (c)) {
				is_capital = FALSE;
			}
			expect_upper = FALSE;
		} else
			expect_upper = TRUE;
	}

	if (has_letter) {
		if (is_upper)
			return SC_Upper;
		if (is_capital)
			return SC_Capital;
	}

	return SC_Other;
}
Пример #2
0
IdePatternSpec *
ide_pattern_spec_new (const gchar *needle)
{
  IdePatternSpec *self;
  const gchar *tmp;

  g_return_val_if_fail (needle, NULL);

  self = g_new0 (IdePatternSpec, 1);
  self->ref_count = 1;
  self->needle = g_strdup (needle);
  self->parts = g_strsplit (needle, " ", 0);
  self->case_sensitive = FALSE;

  for (tmp = needle; tmp; tmp = g_utf8_next_char (tmp))
    {
      if (g_unichar_isupper (g_utf8_get_char (tmp)))
        {
          self->case_sensitive = TRUE;
          break;
        }
    }

  return self;
}
Пример #3
0
static inline bool str_contains_upper(const char *s)
{
	while (*s) {
		if(g_unichar_isupper(g_utf8_get_char(s)))
			return true;
		s = g_utf8_next_char(s);
	}
	return false;
}
Пример #4
0
/* value is the match value suitable for exact match if required */
static int
header_match(const char *value, const char *match, camel_search_match_t how)
{
	const unsigned char *p;
	int vlen, mlen;
	gunichar c;

	if (how == CAMEL_SEARCH_MATCH_SOUNDEX)
		return header_soundex (value, match);

	vlen = strlen(value);
	mlen = strlen(match);
	if (vlen < mlen)
		return FALSE;

	/* from dan the man, if we have mixed case, perform a case-sensitive match,
	   otherwise not */
	p = (const unsigned char *)match;

	c = camel_utf8_getc (&p);
	while (c)
	{
		if (g_unichar_isupper(c)) {
			switch (how) {
			case CAMEL_SEARCH_MATCH_EXACT:
				return strcmp(value, match) == 0;
			case CAMEL_SEARCH_MATCH_CONTAINS:
				return strstr(value, match) != NULL;
			case CAMEL_SEARCH_MATCH_STARTS:
				return strncmp(value, match, mlen) == 0;
			case CAMEL_SEARCH_MATCH_ENDS:
				return strcmp(value + vlen - mlen, match) == 0;
			default:
				break;
			}
			return FALSE;
		}
		c = camel_utf8_getc (&p);
	}

	switch (how) {
	case CAMEL_SEARCH_MATCH_EXACT:
		return camel_ustrcasecmp(value, match) == 0;
	case CAMEL_SEARCH_MATCH_CONTAINS:
		return camel_ustrstrcase(value, match) != NULL;
	case CAMEL_SEARCH_MATCH_STARTS:
		return camel_ustrncasecmp(value, match, mlen) == 0;
	case CAMEL_SEARCH_MATCH_ENDS:
		return camel_ustrcasecmp(value + vlen - mlen, match) == 0;
	default:
		break;
	}

	return FALSE;
}
Пример #5
0
gchar *
mousepad_util_utf8_strcapital (const gchar *str)
{
  gunichar     c;
  const gchar *p;
  gchar       *buf;
  GString     *result;
  gboolean     upper = TRUE;

  g_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);

  /* create a new string */
  result = g_string_sized_new (strlen (str));

  /* walk though the string */
  for (p = str; *p != '\0'; p = g_utf8_next_char (p))
    {
      /* get the unicode char */
      c = g_utf8_get_char (p);

      /* only change the case of alpha chars */
      if (g_unichar_isalpha (c))
        {
          /* check case */
          if (upper ? g_unichar_isupper (c) : g_unichar_islower (c))
            {
              /* currect case is already correct */
              g_string_append_unichar (result, c);
            }
          else
            {
              /* convert the case of the char and append it */
              buf = upper ? g_utf8_strup (p, 1) : g_utf8_strdown (p, 1);
              g_string_append (result, buf);
              g_free (buf);
            }

          /* next char must be lowercase */
          upper = FALSE;
        }
      else
        {
          /* append the char */
          g_string_append_unichar (result, c);

          /* next alpha char uppercase after a space */
          upper = g_unichar_isspace (c);
        }
    }

  /* return the result */
  return g_string_free (result, FALSE);
}
Пример #6
0
static inline gboolean
contains_uppercase_letters (const gchar *str,
                            gint         len)
{
  const gchar *p;

  for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p))
    {
      if (g_unichar_isupper (g_utf8_get_char (p)))
	return TRUE;
    }
  return FALSE;
}
Пример #7
0
static gboolean
str_has_uppercase (const char *str)
{
	while (str != NULL && *str != '\0') {
		gunichar c;

		c = g_utf8_get_char (str);

		if (g_unichar_isupper (c))
			return TRUE;

		str = g_utf8_next_char (str);
	}

	return FALSE;
}
static gdouble
quality_func (MatePasswordDialog *dialog, const char *text, gpointer ptr)
{
	const char *p;
	gsize length;
	int uppercase = 0, symbols = 0, numbers = 0, strength;
	gunichar uc;

	if (text == NULL) return 0.0;

	/* Get the length */
	length = g_utf8_strlen (text, -1);

	/* Count the number of number, symbols and uppercase chars */
	for (p = text; *p; p = g_utf8_find_next_char (p, NULL))
	{
		uc = g_utf8_get_char (p);

		if (g_unichar_isdigit (uc))
		{
			numbers++;
		}
		else if (g_unichar_isupper (uc))
		{
			uppercase++;
		}
		else if (g_unichar_islower (uc))
		{
			/* Not counted */
		}
		else if (g_unichar_isgraph (uc))
		{
			symbols++;
		}
	}

	if (length    > 5) length = 5;
	if (numbers   > 3) numbers = 3;
	if (symbols   > 3) symbols = 3;
	if (uppercase > 3) uppercase = 3;

	strength = (length * 10 - 20) + (numbers * 10) + (symbols * 15) + (uppercase * 10);
	strength = CLAMP (strength, 0, 100);

	return (double) strength / 100.0;
}
Пример #9
0
gchar *
mousepad_util_utf8_stropposite (const gchar *str)
{
  gunichar     c;
  const gchar *p;
  gchar       *buf;
  GString     *result;

  g_return_val_if_fail (g_utf8_validate (str, -1, NULL), NULL);

  /* create a new string */
  result = g_string_sized_new (strlen (str));

  /* walk though the string */
  for (p = str; *p != '\0'; p = g_utf8_next_char (p))
    {
      /* get the unicode char */
      c = g_utf8_get_char (p);

      /* only change the case of alpha chars */
      if (g_unichar_isalpha (c))
        {
          /* get the opposite case of the char */
          if (g_unichar_isupper (c))
            buf = g_utf8_strdown (p, 1);
          else
            buf = g_utf8_strup (p, 1);

          /* append to the buffer */
          g_string_append (result, buf);
          g_free (buf);
        }
      else
        {
          /* append the char */
          g_string_append_unichar (result, c);
        }
    }

  /* return the result */
  return g_string_free (result, FALSE);
}
Пример #10
0
static void
uspell_dict_add_to_session (EnchantDict * me, const char *const word,
		     size_t len)
{
	uSpell *manager;
	wide_t buf[MAXCHARS];
	utf8_t myWord[MAXCHARS];
	int length, index;
	
	manager = reinterpret_cast<uSpell *>(me->user_data);

	manager->acceptWord((const utf8_t *)word);
	if (len >= MAXCHARS)
		return; // too long; can't reasonably convert
	// see if we want to acceptWord(uppercase(myWord))
	if (!(manager->theFlags & uSpell::upperLower)) return; // non-case language
	length = utf8_wide(buf, (const utf8_t *)word, MAXCHARS);
	for (index = 0; index < length; index++) {
		if (g_unichar_isupper(buf[index])) return; // case-sensitive word
		buf[index] = g_unichar_toupper(buf[index]);
	}
	wide_utf8(myWord, MAXCHARS, buf, length);
	manager->acceptWord(myWord);
} // uspell_dict_add_to_session
Пример #11
0
void CheckCapitalization::check_suspicious_capitalization(ustring & text)
/*
Checks on suspicious capitalization, like "bOat" or "BOat".
There are exceptions to this check.
*/
{
  // Load text into buffer.
  ustring text2(text);
  text2.append(" ");
  GtkTextBuffer *textbuffer;
  textbuffer = gtk_text_buffer_new(NULL);
  gtk_text_buffer_set_text(textbuffer, text2.c_str(), -1);
  // Iterators.  
  GtkTextIter startiter, enditer;
  // Check all separate words.
  gtk_text_buffer_get_start_iter(textbuffer, &enditer);
  while (gtk_text_iter_forward_word_end(&enditer)) {
    startiter = enditer;
    gtk_text_iter_backward_word_start(&startiter);
    vector < bool > capspattern;
    unsigned int capscount = 0;
    GtkTextIter iter = startiter;
    while (gtk_text_iter_in_range(&iter, &startiter, &enditer)) {
      bool upper = g_unichar_isupper(gtk_text_iter_get_char(&iter));
      capspattern.push_back(upper);
      if (upper)
        capscount++;
      gtk_text_iter_forward_char(&iter);
    }
    // No further checking if words are too short.
    if (capspattern.size() < 2)
      continue;
    // No further checking if only small letters.
    if (capscount == 0)
      continue;
    // No further checking if all capitals.
    if (capscount == capspattern.size())
      continue;
    // No further checking if first letter capitalized only.
    if ((capspattern[0]) && (capscount == 1))
      continue;
    // Ok, there could be a problem of mixed capitalization.
    // Get the prefix before the first capital, and the suffix after it.
    ustring word = gtk_text_iter_get_text(&startiter, &enditer);
    ustring uncapitalized_prefix;
    ustring capitalized_suffix;
    for (unsigned int i = 1; i < capspattern.size(); i++) {
      if (capspattern[i]) {
        uncapitalized_prefix = word.substr(0, i);
        capitalized_suffix = word.substr(i, word.length() - i);
        break;
      }
    }
    // See whether the suffix is properly capitalized.
    unsigned int suffix_capital_count = 0;
    for (unsigned int i = 0; i < capitalized_suffix.length(); i++) {
      if (g_unichar_isupper(g_utf8_get_char(capitalized_suffix.substr(i, 1).c_str())))
        suffix_capital_count++;
    }
    bool suffix_properly_capitalized = false;
    if (suffix_capital_count == 1)
      suffix_properly_capitalized = true;
    if (suffix_capital_count == capitalized_suffix.length())
      suffix_properly_capitalized = true;
    // Give message and continue if capitalization error in suffix, but only
    // if this so-called wrongly capitalized suffix has not been approved af.
    if (!suffix_properly_capitalized) {
      if (capitalized_suffixes.find(capitalized_suffix) == capitalized_suffixes.end()) {
        mixed_capitalization_message(word);
        continue;
      }
    }
    // No further checking if this uncapitalized prefix is in the list,
    // or any is allowed.
    if (uncapitalized_prefixes.find(uncapitalized_prefix) != uncapitalized_prefixes.end())
      continue;
    if (allow_any_uncapitalized_prefixes)
      continue;
    // Ok, not in the list. Try again with lower case initial.
    ustring initial = uncapitalized_prefix.substr(0, 1);
    initial = initial.casefold();
    uncapitalized_prefix.replace(0, 1, initial);
    if (uncapitalized_prefixes.find(uncapitalized_prefix) != uncapitalized_prefixes.end())
      continue;
    // No further checking if the suffix is in the list of approved suffixes.
    if (capitalized_suffixes.find(capitalized_suffix) != capitalized_suffixes.end())
      continue;
    // Ok, not found, but it could be this suffix is in all capitals. Handle that.
    initial = capitalized_suffix.substr(0, 1);
    capitalized_suffix.erase(0, 1);
    capitalized_suffix = capitalized_suffix.casefold();
    capitalized_suffix.insert(0, initial);
    if (capitalized_suffixes.find(capitalized_suffix) != capitalized_suffixes.end())
      continue;
    // Ok, it appears we've got an error here -> give message.
    mixed_capitalization_message(word);
  }
  // Free memory
  g_object_unref(textbuffer);
}
Пример #12
0
void CheckCapitalization::check_capitalization(vector < int >&chapters, vector < ustring > &verses, ustring & text, vector < size_t > &pointers, bool end_check)
/*
Check capitalization in text.
If "end_check" is true, it also check for final sentence closing.
*/
{
  /*
     Note that this at first used gtk_text_iter_starts_sentence (&iter) and
     gtk_text_iter_ends_sentence (&iter), but these functions are not good enough,
     because do not work in several cases, like e.g. in the following line, it does
     not indicate the end of the sentence:
     As soon as the leaders of the tribes of Israel took their places, the 
     Israelites said, “How could such a horrible thing happen?"
     Therefore we use other means to check sentences.
   */

  // No check if there's no text.
  if (trim(text).empty())
    return;
  // Some variables needed.
  bool expect_capital_now = false;
  bool expect_capital_caused_by_reference = false;
  gunichar previous_char = 0;
  int localchapter = 0;
  ustring localverse = "0";
  GtkTextBuffer *textbuffer;
  textbuffer = gtk_text_buffer_new(NULL);
  gtk_text_buffer_set_text(textbuffer, text.c_str(), -1);
  GtkTextIter iter;
  gtk_text_buffer_get_start_iter(textbuffer, &iter);
  bool going = true;
  while (going) {
    // Get the unicode character.
    gunichar unichar = gtk_text_iter_get_char(&iter);
    // See whether to expect a capital now.
    if (punctuation_followed_by_capitals_set.find(unichar) != punctuation_followed_by_capitals_set.end()) {
      // Ok, expect capital.
      expect_capital_now = true;
      expect_capital_caused_by_reference = false;
      // Was this expectation caused by a reference?
      if (is_reference(iter))
        expect_capital_caused_by_reference = true;
    }
    // If we expect a capital, and we find one, no longer look for one.
    if (expect_capital_now) {
      if (g_unichar_isupper(unichar)) {
        expect_capital_now = false;
      }
    }
    // If we expect a capital, and we get lower case, that might be trouble.
    if (expect_capital_now) {
      if (g_unichar_islower(unichar)) {
        // There is no trouble if it follows a character after which to ignore lower case.
        if (ignore_lower_case_following_set.find(previous_char) != ignore_lower_case_following_set.end()) {
          expect_capital_now = false;
        }
        // If the lowercase character follows an abbreviation, there is no trouble either.
        GtkTextIter iter2 = iter;
        gtk_text_iter_backward_word_start(&iter2);
        GtkTextIter iter3 = iter2;
        gtk_text_iter_forward_word_end(&iter3);
        gtk_text_iter_forward_char(&iter3);
        ustring abbreviation = gtk_text_iter_get_text(&iter2, &iter3);
        if (abbreviations.find(abbreviation) != abbreviations.end()) {
          expect_capital_now = false;
        }
        // If it follows a reference, there is no trouble.
        if (expect_capital_caused_by_reference)
          expect_capital_now = false;
        // Ok, give message.
        if (expect_capital_now) {
          // Determine chapter and verse.
          get_chapter_and_verse(chapters, verses, pointers, iter, localchapter, localverse);
          message(book, localchapter, localverse, _("Capital expected: ") + get_context(iter));
        }
        // Only give one message about missing capitals in this context.
        expect_capital_now = false;
      }
    }
    // Store this characters as the previous characters for the next round.
    if (g_unichar_isgraph(unichar))
      previous_char = unichar;
    // Next round.
    going = gtk_text_iter_forward_char(&iter);
  }
  // The sentence should be ended with proper punctuation.
  if (end_check) {
    if (expect_capital_now)
      if (g_unichar_isdigit(previous_char))
        expect_capital_now = false;
    if (!expect_capital_now) {
      message(book, chapter, verse, _("Unended sentence: ") + get_context(iter));
    }
  }
  // Free memory
  g_object_unref(textbuffer);
}