summaryrefslogtreecommitdiffhomepage
path: root/dev/CompilerKit/src/FrontendHelpers.cc
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-08-16 19:55:22 +0200
committerGitHub <noreply@github.com>2025-08-16 19:55:22 +0200
commit5467549fa5d656afd0c6bf12c6c3928a8c919591 (patch)
tree755d6bf288f0bd474641d9ace1de290991feffee /dev/CompilerKit/src/FrontendHelpers.cc
parent433bb5ef102b2bfa0049468be00d63011da8b973 (diff)
parent0c14f7cff6535d110bc95a7699db043d8aa9aa1a (diff)
Merge pull request #9 from nekernel-org/dev
v0.0.3
Diffstat (limited to 'dev/CompilerKit/src/FrontendHelpers.cc')
-rw-r--r--dev/CompilerKit/src/FrontendHelpers.cc51
1 files changed, 51 insertions, 0 deletions
diff --git a/dev/CompilerKit/src/FrontendHelpers.cc b/dev/CompilerKit/src/FrontendHelpers.cc
new file mode 100644
index 0000000..37b36f7
--- /dev/null
+++ b/dev/CompilerKit/src/FrontendHelpers.cc
@@ -0,0 +1,51 @@
+/* -------------------------------------------
+
+ Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved
+
+------------------------------------------- */
+
+#include <CompilerKit/Frontend.h>
+
+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