Word frequency: Difference between revisions

m
C - return value of g_mapped_file_get_contents isn't guaranteed to be zero-terminated
m (C - return value of g_mapped_file_get_contents isn't guaranteed to be zero-terminated)
Line 1,019:
 
bool get_top_words(const char* filename, size_t count) {
GError* error = NULL;
GMappedFile* mapped_file = g_mapped_file_new(filename, FALSE, NULL&error);
if (mapped_file == NULL) {
fprintf(stderr, "Cannot open file %s\n", filenameerror->message);
g_error_free(error);
return false;
}
Line 1,030 ⟶ 1,032:
return false;
}
gsize file_size = g_mapped_file_get_length(mapped_file);
// Store word counts in a hash table
GHashTable* ht = g_hash_table_new_full(g_str_hash, g_str_equal,
Line 1,035 ⟶ 1,038:
GRegex* regex = g_regex_new("\\w+", 0, 0, NULL);
GMatchInfo* match_info;
g_regex_matchg_regex_match_full(regex, text, file_size, 0, 0, &match_info, NULL);
while (g_match_info_matches(match_info)) {
char* word = g_match_info_fetch(match_info, 0);
Line 1,053 ⟶ 1,056:
g_match_info_free(match_info);
g_regex_unref(regex);
g_mapped_file_unref(mapped_file);
 
// Sort words in decreasing order of frequency
Line 1,075 ⟶ 1,079:
g_free(words);
g_hash_table_destroy(ht);
g_mapped_file_unref(mapped_file);
return true;
}
1,777

edits