[easytag] Improve scanner functions and fix tests
- From: David King <davidk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [easytag] Improve scanner functions and fix tests
- Date: Mon, 7 Apr 2014 20:24:48 +0000 (UTC)
commit f43c0a65e1404535c5a11a0caf7576a69a576f70
Author: Abhinav <abhijangda hotmail com>
Date: Mon Apr 7 21:08:59 2014 +0100
Improve scanner functions and fix tests
https://bugzilla.gnome.org/show_bug.cgi?id=726108
src/easytag.c | 31 +++++------
src/scan.c | 152 +++++++++++++++++++++++------------------------------
src/scan.h | 8 ++--
src/scan_dialog.c | 33 ++++++++++--
tests/test-scan.c | 28 ++++++----
5 files changed, 129 insertions(+), 123 deletions(-)
---
diff --git a/src/easytag.c b/src/easytag.c
index 3f29ed3..a03478b 100644
--- a/src/easytag.c
+++ b/src/easytag.c
@@ -4309,21 +4309,23 @@ Convert_All_Uppercase (GtkWidget *entry)
static void
Convert_All_Lowercase (GtkWidget *entry)
{
- gchar *string = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
+ gchar *res;
+ const gchar *string = gtk_entry_get_text (GTK_ENTRY (entry));
- Scan_Process_Fields_All_Downcase(string);
- gtk_entry_set_text(GTK_ENTRY(entry),string);
- g_free(string);
+ res = Scan_Process_Fields_All_Uppercase (string);
+ gtk_entry_set_text (GTK_ENTRY (entry), res);
+ g_free (res);
}
static void
Convert_Letter_Uppercase (GtkWidget *entry)
{
- gchar *string = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
+ gchar *res;
+ const gchar *string = gtk_entry_get_text (GTK_ENTRY (entry));
- Scan_Process_Fields_Letter_Uppercase(string);
- gtk_entry_set_text(GTK_ENTRY(entry),string);
- g_free(string);
+ res = Scan_Process_Fields_Letter_Uppercase (string);
+ gtk_entry_set_text (GTK_ENTRY (entry), res);
+ g_free (res);
}
static void
@@ -4349,15 +4351,12 @@ Convert_Remove_Space (GtkWidget *entry)
static void
Convert_Insert_Space (GtkWidget *entry)
{
- // FIX ME : we suppose that it will not grow more than 2 times its size...
- guint string_length = 2 * strlen(gtk_entry_get_text(GTK_ENTRY(entry)));
- gchar *string = g_malloc(string_length+1);
- strncpy(string,gtk_entry_get_text(GTK_ENTRY(entry)),string_length);
- string[string_length]='\0';
+ gchar *res;
+ const gchar *string = (gtk_entry_get_text (GTK_ENTRY (entry)));
- Scan_Process_Fields_Insert_Space(&string);
- gtk_entry_set_text(GTK_ENTRY(entry),string);
- g_free(string);
+ res = Scan_Process_Fields_Insert_Space (string);
+ gtk_entry_set_text (GTK_ENTRY (entry), res);
+ g_free (res);
}
static void
diff --git a/src/scan.c b/src/scan.c
index 10419ca..cc74e04 100644
--- a/src/scan.c
+++ b/src/scan.c
@@ -84,39 +84,36 @@ Scan_Process_Fields_Remove_Space (gchar *string)
}
/*
- * The function inserts a space before an uppercase letter
- * It is needed to realloc the memory!
+ * Scan_Process_Fields_Insert_Space:
+ * @string: Input string
+ *
+ * This function will insert space before every uppercase character.
+ *
+ * Returns: A newly allocated string.
*/
-void
-Scan_Process_Fields_Insert_Space (gchar **string)
+gchar *
+Scan_Process_Fields_Insert_Space (const gchar *string)
{
gchar *iter;
gunichar c;
- gint j;
- guint string_length;
- gchar *string1;
-
- // FIX ME : we suppose that it will not grow more than 2 times its size...
- string_length = 2 * strlen(*string);
- //string1 = g_realloc(*string, string_length+1);
- string1 = g_malloc(string_length+1);
- strncpy(string1,*string,string_length);
- string1[string_length]='\0';
- g_free(*string);
- *string = string1;
-
- for (iter = g_utf8_next_char(*string); *iter; iter = g_utf8_next_char(iter)) // At start :
g_utf8_next_char to not consider first "uppercase" letter
+ GString *string1;
+
+ string1 = g_string_new ("");
+ g_string_append_c (string1, *string);
+
+ for (iter = g_utf8_next_char (string); *iter; iter = g_utf8_next_char (iter))
{
- c = g_utf8_get_char(iter);
+ c = g_utf8_get_char (iter);
- if (g_unichar_isupper(c))
+ if (g_unichar_isupper (c))
{
- for (j = strlen(iter); j > 0; j--)
- *(iter + j) = *(iter + j - 1);
- *iter = ' ';
- iter++;
+ g_string_append_c (string1, ' ');
}
+
+ g_string_append_unichar (string1, c);
}
+
+ return g_string_free (string1, FALSE);
}
/*
@@ -166,86 +163,69 @@ Scan_Remove_Spaces (gchar *string)
}
}
-void
-Scan_Process_Fields_All_Uppercase (gchar *string)
+/* Returns a newly-allocated string. */
+gchar *
+Scan_Process_Fields_All_Uppercase (const gchar *string)
{
- gchar *temp;
- gchar temp2[6]; // Must have at least 6 bytes of space
- gunichar c;
-
- for (temp = string; *temp; temp = g_utf8_next_char(temp))
- {
- c = g_utf8_get_char(temp);
- if (g_unichar_islower(c))
- strncpy(temp, temp2, g_unichar_to_utf8(g_unichar_toupper(c), temp2));
- }
+ return g_utf8_strup (string, -1);
}
-void
-Scan_Process_Fields_All_Downcase (gchar *string)
+/* Returns a newly-allocated string. */
+gchar *
+Scan_Process_Fields_All_Downcase (const gchar *string)
{
- gchar *temp;
- gchar temp2[6];
- gunichar c;
-
- for (temp = string; *temp; temp = g_utf8_next_char(temp))
- {
- c = g_utf8_get_char(temp);
- if (g_unichar_isupper(c))
- strncpy(temp, temp2, g_unichar_to_utf8(g_unichar_tolower(c), temp2));
- }
+ return g_utf8_strdown (string, -1);
}
-void
-Scan_Process_Fields_Letter_Uppercase (gchar *string)
+/* Returns a newly-allocated string. */
+gchar *
+Scan_Process_Fields_Letter_Uppercase (const gchar *string)
{
- gchar *temp;
+ const gchar *temp;
gchar temp2[6];
gboolean set_to_upper_case = TRUE;
gunichar c;
- gchar utf8_character[6];
- gchar *word, *word1, *word2;
+ GString *string1;
+
+ string1 = g_string_new ("");
- for (temp = string; *temp; temp = g_utf8_next_char(temp))
+ for (temp = string; *temp; temp = g_utf8_next_char (temp))
{
- c = g_utf8_get_char(temp);
- if (set_to_upper_case && g_unichar_islower(c))
- strncpy(temp, temp2, g_unichar_to_utf8(g_unichar_toupper(c), temp2));
- else if (!set_to_upper_case && g_unichar_isupper(c))
- strncpy(temp, temp2, g_unichar_to_utf8(g_unichar_tolower(c), temp2));
- set_to_upper_case = FALSE; // After the first time, all will be down case
- }
+ gchar *temp3;
+ int l;
- temp = string;
+ c = g_utf8_get_char (temp);
+ l = g_unichar_to_utf8 (c, temp2);
- // Uppercase again the word 'I' in english
- while ( temp )
- {
- word = temp; // Needed if there is only one word
- word1 = g_utf8_strchr(temp,-1,' ');
- word2 = g_utf8_strchr(temp,-1,'_');
-
- // Take the first string found (near beginning of string)
- if (word1 && word2)
- word = MIN(word1,word2);
- else if (word1)
- word = word1;
- else if (word2)
- word = word2;
+ if (set_to_upper_case && g_unichar_islower(c))
+ {
+ temp3 = g_utf8_strup (temp2, l);
+ g_string_append (string1, temp3);
+ g_free (temp3);
+ }
+ else if (!set_to_upper_case && g_unichar_isupper(c))
+ {
+ temp3 = g_utf8_strdown (temp2, l);
+ g_string_append (string1, temp3);
+ g_free (temp3);
+ }
else
- // Last word of the string
- break;
-
- // Go to first character of the word (char. after ' ' or '_')
- word = word+1;
+ {
+ g_string_append_len (string1, temp2, l);
+ }
- // Set uppercase word 'I'
- if (g_ascii_strncasecmp("I ", word, strlen("I ")) == 0)
+ /* Uppercase the word 'I' in english */
+ if (!set_to_upper_case &&
+ (*(temp - 1) == ' ' || *(temp - 1) == '_') &&
+ (*temp == 'i' || *temp == 'I') &&
+ (*(temp + 1) == ' ' || *(temp + 1) == '_'))
{
- c = g_utf8_get_char(word);
- strncpy(word, utf8_character, g_unichar_to_utf8(g_unichar_toupper(c), utf8_character));
+ string1->str [string1->len - 1] = 'I';
}
- temp = word;
+ /* After the first time, all will be lower case. */
+ set_to_upper_case = FALSE;
}
+
+ return g_string_free (string1, FALSE);
}
diff --git a/src/scan.h b/src/scan.h
index addd242..8c54686 100644
--- a/src/scan.h
+++ b/src/scan.h
@@ -25,11 +25,11 @@ void Scan_Convert_Underscore_Into_Space (gchar *string);
void Scan_Convert_P20_Into_Space (gchar *string);
void Scan_Convert_Space_Into_Underscore (gchar *string);
void Scan_Process_Fields_Remove_Space (gchar *string);
-void Scan_Process_Fields_Insert_Space (gchar **string);
+gchar* Scan_Process_Fields_Insert_Space (const gchar *string);
void Scan_Process_Fields_Keep_One_Space (gchar *string);
void Scan_Remove_Spaces (gchar *string);
-void Scan_Process_Fields_All_Uppercase (gchar *string);
-void Scan_Process_Fields_All_Downcase (gchar *string);
-void Scan_Process_Fields_Letter_Uppercase (gchar *string);
+gchar* Scan_Process_Fields_All_Uppercase (const gchar *string);
+gchar* Scan_Process_Fields_All_Downcase (const gchar *string);
+gchar* Scan_Process_Fields_Letter_Uppercase (const gchar *string);
#endif /* !ET_SCAN_H_ */
diff --git a/src/scan_dialog.c b/src/scan_dialog.c
index 8ce1e04..7b2c4b4 100644
--- a/src/scan_dialog.c
+++ b/src/scan_dialog.c
@@ -1363,7 +1363,12 @@ Scan_Process_Fields_Functions (gchar **string)
Scan_Convert_Space_Into_Underscore (*string);
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsInsertSpace)))
- Scan_Process_Fields_Insert_Space(string);
+ {
+ gchar *res;
+ res = Scan_Process_Fields_Insert_Space (*string);
+ g_free (*string);
+ *string = res;
+ }
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsOnlyOneSpace)))
Scan_Process_Fields_Keep_One_Space(*string);
@@ -1372,13 +1377,28 @@ Scan_Process_Fields_Functions (gchar **string)
Scan_Convert_Character(string);
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsAllUppercase)))
- Scan_Process_Fields_All_Uppercase(*string);
+ {
+ gchar *res;
+ res = Scan_Process_Fields_All_Uppercase (*string);
+ g_free (*string);
+ *string = res;
+ }
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsAllDowncase)))
- Scan_Process_Fields_All_Downcase(*string);
+ {
+ gchar *res;
+ res = Scan_Process_Fields_All_Downcase (*string);
+ g_free (*string);
+ *string = res;
+ }
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsFirstLetterUppercase)))
- Scan_Process_Fields_Letter_Uppercase(*string);
+ {
+ gchar *res;
+ res = Scan_Process_Fields_Letter_Uppercase (*string);
+ g_free (*string);
+ *string = res;
+ }
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ProcessFieldsFirstLettersUppercase)))
Scan_Process_Fields_First_Letters_Uppercase(*string);
@@ -1452,7 +1472,10 @@ void Scan_Process_Fields_First_Letters_Uppercase (gchar *string)
{
exempt[0] = NULL;
}
- Scan_Process_Fields_All_Downcase(string);
+
+ temp = Scan_Process_Fields_All_Downcase (string);
+ g_free (string);
+ string = temp;
if (!g_utf8_validate(string,-1,NULL))
{
diff --git a/tests/test-scan.c b/tests/test-scan.c
index 30ff8ba..34f3235 100644
--- a/tests/test-scan.c
+++ b/tests/test-scan.c
@@ -102,13 +102,14 @@ scan_insert_space (void)
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
- gchar *string;
+ gchar *string, *res;
string = g_strdup (cases[i]);
- Scan_Process_Fields_Insert_Space (&string);
- check_string (string, results[i]);
+ res = Scan_Process_Fields_Insert_Space (string);
+ check_string (res, results[i]);
g_free (string);
+ g_free (res);
}
}
@@ -123,13 +124,14 @@ scan_all_uppercase (void)
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
- gchar *string;
+ gchar *string, *res;
string = g_strdup (cases[i]);
- Scan_Process_Fields_All_Uppercase (string);
- check_string (string, results[i]);
+ res = Scan_Process_Fields_All_Uppercase (string);
+ check_string (res, results[i]);
g_free (string);
+ g_free (res);
}
}
@@ -144,13 +146,14 @@ scan_all_lowercase (void)
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
- gchar *string;
+ gchar *string, *res;
string = g_strdup (cases[i]);
- Scan_Process_Fields_All_Downcase (string);
- check_string (string, results[i]);
+ res = Scan_Process_Fields_All_Downcase (string);
+ check_string (res, results[i]);
g_free (string);
+ g_free (res);
}
}
@@ -167,13 +170,14 @@ scan_letter_uppercase (void)
for (i = 0; i < G_N_ELEMENTS (cases); i++)
{
- gchar *string;
+ gchar *string, *res;
string = g_strdup (cases [i]);
- Scan_Process_Fields_Letter_Uppercase (string);
- check_string (string, results [i]);
+ res = Scan_Process_Fields_Letter_Uppercase (string);
+ check_string (res, results [i]);
g_free (string);
+ g_free (res);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]