diff --git a/examples/cheap_grep.cpp b/examples/cheap_grep.cpp index 607cd60..1afb506 100644 --- a/examples/cheap_grep.cpp +++ b/examples/cheap_grep.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -18,11 +17,11 @@ auto string_deleter = [](lite_string *ls) -> void { string_free(ls); }; * @param ignoreCase Whether to ignore the case of the characters. * @return 0 if the pattern is found, 1 otherwise. */ -int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase) { +int cheap_grep(const lite_string *pattern, std::istream &input, const bool ignoreCase) { // Unique pointers to manage lite_string objects. - std::unique_ptr s(string_new(), string_deleter); - std::unique_ptr patternCopy(string_new(), string_deleter); - std::unique_ptr lineCopy(string_new(), string_deleter); + const std::unique_ptr s(string_new(), string_deleter); + const std::unique_ptr patternCopy(string_new(), string_deleter); + const std::unique_ptr lineCopy(string_new(), string_deleter); string_copy(pattern, patternCopy.get()); if (ignoreCase) string_to_lower(patternCopy.get()); @@ -36,7 +35,7 @@ int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase) if (ignoreCase) string_to_lower(lineCopy.get()); - if (string_find(lineCopy.get(), patternCopy.get()) != SIZE_MAX) { + if (string_find(lineCopy.get(), patternCopy.get()) != lite_string_npos) { ret = 0; std::cout << line << '\n'; } @@ -47,7 +46,7 @@ int cheap_grep(const lite_string *pattern, std::istream &input, bool ignoreCase) return ret; } -int main(int argc, char *argv[]) { +int main(const int argc, char *argv[]) { if (argc < 3) { std::cerr << "Usage: " << argv[0] << " [-i] \n"; return 1; @@ -55,7 +54,7 @@ int main(int argc, char *argv[]) { bool ignoreCase = false; int argIndex = 1; - std::unique_ptr firstArg(string_new_cstr(argv[1]), string_deleter); + const std::unique_ptr firstArg(string_new_cstr(argv[1]), string_deleter); if (argc > 3 && string_compare_cstr(firstArg.get(), "-i")) { ignoreCase = true; @@ -63,19 +62,20 @@ int main(int argc, char *argv[]) { } // Create smart pointers to manage the lifetime of the lite_string objects. - std::unique_ptr pattern(string_new_cstr(argv[argIndex]), string_deleter); - std::unique_ptr filename(string_new_cstr(argv[argIndex + 1]), - string_deleter); + const std::unique_ptr pattern( + string_new_cstr(argv[argIndex]), string_deleter); + const std::unique_ptr filename( + string_new_cstr(argv[argIndex + 1]), string_deleter); // If the filename is "-", read from stdin. - if (string_compare_cstr(filename.get(), "-")) { + if (string_compare_cstr(filename.get(), "-")) return cheap_grep(pattern.get(), std::cin, ignoreCase); - } else { - std::ifstream file(string_cstr(filename.get())); - if (!file.is_open()) { - std::cerr << "Error: Unable to open file: " << std::quoted(string_cstr(filename.get())) << std::endl; - return 1; - } - return cheap_grep(pattern.get(), file, ignoreCase); + + // Open the file and read from it. + std::ifstream file(string_cstr(filename.get())); + if (!file.is_open()) { + std::cerr << "Error: Unable to open file: " << std::quoted(string_cstr(filename.get())) << std::endl; + return 1; } + return cheap_grep(pattern.get(), file, ignoreCase); } diff --git a/examples/word_stats.c b/examples/word_stats.c index cfe0e8b..2b0feab 100644 --- a/examples/word_stats.c +++ b/examples/word_stats.c @@ -4,7 +4,7 @@ #include "../lite_string.h" // A simple program that reads a file and prints the number of words and characters in the file. -int main(int argc, char *argv[]) { +int main(const int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) { return 1; } // If the file size is greater than 1 MB, return an error. - size_t file_size = st.st_size; + const size_t file_size = st.st_size; if (file_size > 1 << 20) { fputs("Error: File size is too large.\n", stderr); string_free(s); @@ -76,11 +76,9 @@ int main(int argc, char *argv[]) { } } - double avg_word_length = (double) char_count / word_count; - printf("Word count: %zu\n", word_count); printf("Character count: %zu\n", char_count); - printf("Average word length: %.2f\n", avg_word_length); + printf("Average word length: %.2f\n", (double) char_count / word_count); string_free(s); return 0;