diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-15 22:35:55 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-15 22:35:55 +0100 |
| commit | 730b76e1aae96f9a5cafc13634d6f014e598abaf (patch) | |
| tree | b9c6542602d4fd0138eb7a88e8a9a9d13971e88e /dev/CompilerKit/src/Frontend.cc | |
| parent | 49064df5303b9a96f6b11dd0aed27dc1e269aa9e (diff) | |
feat: API and codebase tweaks.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/CompilerKit/src/Frontend.cc')
| -rw-r--r-- | dev/CompilerKit/src/Frontend.cc | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/dev/CompilerKit/src/Frontend.cc b/dev/CompilerKit/src/Frontend.cc new file mode 100644 index 0000000..ef7b79f --- /dev/null +++ b/dev/CompilerKit/src/Frontend.cc @@ -0,0 +1,61 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include <CompilerKit/Frontend.h> + +/** + * @file Frontend.cc + * @author Amlal El Mahrouss (amlal@nekernel.org) + * @brief Frontend API of NeCTI + * @version 0.0.2 + * + * @copyright Copyright (c) 2025 Amlal El Mahrouss and NeKernel.org Contributors + * + */ + +namespace CompilerKit { +/// 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(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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. +SizeType find_word_range(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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 STLString::npos; +} +} // namespace CompilerKit
\ No newline at end of file |
