diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-29 10:48:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-29 10:48:12 +0200 |
| commit | 433bb5ef102b2bfa0049468be00d63011da8b973 (patch) | |
| tree | e0893a30710477045a5bb085cb7a27aada425c14 /dev/LibCompiler/src/Frontend.cc | |
| parent | 1ddeab9a4426abd781a5066ba79af2ba64de11d9 (diff) | |
| parent | 756ee7f8dc954e27350fe5bdfbaa83b9f69780c8 (diff) | |
Merge pull request #6 from nekernel-org/dev
0.0.2e3
Diffstat (limited to 'dev/LibCompiler/src/Frontend.cc')
| -rw-r--r-- | dev/LibCompiler/src/Frontend.cc | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/dev/LibCompiler/src/Frontend.cc b/dev/LibCompiler/src/Frontend.cc new file mode 100644 index 0000000..69ff6fd --- /dev/null +++ b/dev/LibCompiler/src/Frontend.cc @@ -0,0 +1,51 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include <LibCompiler/Frontend.h> + +namespace LibCompiler { +/// find the perfect matching word in a haystack. +/// \param haystack base string +/// \param needle the string we search for. +/// \return if we found it or not. +bool find_word(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + // declare lambda + auto not_part_of_word = [&](int index) { + if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; + + if (index <= 0 || index >= haystack.size()) return true; + + return false; + }; + + return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); +} + +/// find a word within strict conditions and returns a range of it. +/// \param haystack +/// \param needle +/// \return position of needle. +std::size_t find_word_range(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + if (!isalnum((haystack[index + needle.size() + 1])) && + !isdigit(haystack[index + needle.size() + 1]) && + !isalnum((haystack[index - needle.size() - 1])) && + !isdigit(haystack[index - needle.size() - 1])) { + return index; + } + + return std::string::npos; +} +} // namespace LibCompiler
\ No newline at end of file |
