Teacup rim text: Difference between revisions

m
C - simplified code
(C - use GLib)
m (C - simplified code)
Line 89:
}
return count > 0;
}
 
void destroy_string(gpointer key) {
g_string_free((GString*)key, TRUE);
}
 
int string_compare(gconstpointer p1, gconstpointer p2) {
GStringconst char** s1 = (GStringconst char**)p1;
GStringconst char** s2 = (GStringconst char**)p2;
return strcmp((*s1)->str, (*s2)->str);
}
 
Line 107 ⟶ 103:
return NULL;
}
GPtrArray* dict = g_ptr_array_new_full(1024, destroy_stringg_free);
GString* line = g_string_sized_new(64);
while (get_line(in, line))
g_ptr_array_add(dict, g_string_newg_strdup(line->str));
g_ptr_array_sort(dict, string_compare);
g_string_free(line, TRUE);
Line 123 ⟶ 119:
}
 
bool dictionary_search(const GPtrArray* dictionary, const GStringchar* word) {
return bsearch(&word, dictionary->pdata, dictionary->len, sizeof(GStringchar*),
string_compare) != NULL;
}
 
void find_teacup_words(GPtrArray* dictionary) {
GHashTable* found = g_hash_table_new_full((GHashFunc)g_string_hashg_str_hash, g_str_equal,
(GEqualFunc)g_string_equalg_free, NULL);
GPtrArray* teacup_words = g_ptr_array_new_full(8, destroy_stringg_free);
destroy_string, NULL);
GPtrArray* teacup_words = g_ptr_array_new_full(8, destroy_string);
for (size_t i = 0, n = dictionary->len; i < n; ++i) {
GStringconst char* word = g_ptr_array_index(dictionary, i);
size_t len = strlen(word->len);
if (len < 3 || g_hash_table_contains(found, word))
continue;
g_ptr_array_set_size(teacup_words, 0);
GStringchar* temp = g_string_newg_strdup(word->str);
for (size_t i = 0; i < len - 1; ++i) {
rotate(temp->str, len);
if (g_string_equalstrcmp(word, temp) == 0
|| !dictionary_search(dictionary, temp))
break;
g_ptr_array_add(teacup_words, g_string_newg_strdup(temp->str));
}
g_string_freeg_free(temp, TRUE);
if (teacup_words->len == len - 1) {
printf("%s", word->str);
g_hash_table_insert(found, g_string_newg_strdup(word->str), NULL);
for (size_t i = 0; i < len - 1; ++i) {
GStringconst char* teacup_word = g_ptr_array_index(teacup_words, i);
printf(" %s", teacup_word->str);
g_hash_table_insertg_hash_table_add(found, g_string_newg_strdup(teacup_word->str), NULL);
}
printf("\n");
1,777

edits