Skip to content

Commit 3f710e0

Browse files
committed
add fuzzy search algorithm to stdlib, improve docstrings
1 parent a894b8a commit 3f710e0

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit a894b8a86cfa6d45f0543898f8aaf514107e77b4
2+
Author: Alexeev Bronislav <[email protected]>
3+
Date: Sun Aug 31 17:40:31 2025 +0700
4+
5+
update kintsugi kernel libc
6+
17
commit 2535193c9565f7faff972fd65cee788d23e93431
28
Author: Alexeev Bronislav <[email protected]>
39
Date: Sun Aug 31 17:01:37 2025 +0700

format-code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import subprocess
33
import argparse
44

5+
count = 0
6+
57

68
def find_source_files(root_dir, ignore_dirs):
79
source_files = []
@@ -16,11 +18,13 @@ def find_source_files(root_dir, ignore_dirs):
1618

1719

1820
def format_files(files, clang_format, style):
21+
global count
1922
for file in files:
2023
try:
2124
cmd = [clang_format, "-i", "--style", style, file]
2225
subprocess.run(cmd, check=True)
2326
print(f"\033[32mFormatted:\033[0m {file}")
27+
count += 1
2428
except subprocess.CalledProcessError as e:
2529
print(f"\033[31mError formatting {file}:\033[0m {e}")
2630

@@ -54,7 +58,7 @@ def main():
5458

5559
print(f"\033[33mFound {len(source_files)} files to format:\033[0m")
5660
format_files(source_files, args.clang_format, args.style)
57-
print("\033[32mFormatting complete!\033[0m")
61+
print(f"\033[32mFormatting complete ({count} files)!\033[0m")
5862

5963

6064
if __name__ == "__main__":

src/kernel/kklibc/ctypes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*------------------------------------------------------------------------------
2+
* Kintsugi OS KKLIBC source code
3+
* File: kklibc/ctypes.c
4+
* Title: Библиотека для работы с типами C
5+
* Description: null
6+
* ----------------------------------------------------------------------------*/
7+
18
#include "ctypes.h"
29

310
/**

src/kernel/kklibc/ctypes.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*------------------------------------------------------------------------------
2+
* Kintsugi OS KKLIBC source code
3+
* File: kklibc/ctypes.h
4+
* Title: Библиотека для работы с типами C (заголовочный файл)
5+
* Description: null
6+
* ----------------------------------------------------------------------------*/
7+
18
#ifndef CTYPES_H
29
#define CTYPES_H
310

@@ -13,6 +20,15 @@ typedef char s8;
1320
#define low_16(address) (u16)((address) & 0xFFFF)
1421
#define high_16(address) (u16)(((address) >> 16) & 0xFFFF)
1522

23+
#define KB (1024)
24+
#define MB (1024 * 1024)
25+
#define GB (1024 * 1024 * 1024)
26+
#define TB (1024 * 1024 * 1024 * 1024)
27+
#define PB (1024 * 1024 * 1024 * 1024 * 1024)
28+
#define EB (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
29+
#define ZB (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
30+
#define YB (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
31+
1632
int isalnum(int c);
1733
int isalpha(int c);
1834
int isblank(int c);

src/kernel/kklibc/datatypes/vector.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*------------------------------------------------------------------------------
2+
* Kintsugi OS KKLIBC source code
3+
* File: kklibc/datatypes/vector.c
4+
* Title: Библиотека для работы с векторами
5+
* Description: null
6+
* ----------------------------------------------------------------------------*/
17
#include "vector.h"
28

39
#include "../mem.h"

src/kernel/kklibc/datatypes/vector.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*------------------------------------------------------------------------------
2+
* Kintsugi OS KKLIBC source code
3+
* File: kklibc/datatypes/vector.h
4+
* Title: Библиотека для работы с векторами (заголовочный файл)
5+
* Description: null
6+
* ----------------------------------------------------------------------------*/
17
#ifndef DATATYPE_VECTOR_H
28
#define DATATYPE_VECTOR_H
39

@@ -9,7 +15,20 @@ typedef struct vector {
915
u32 capacity;
1016
} vector_t;
1117

18+
/**
19+
* @brief Создать вектор
20+
*
21+
* @param initial_capacity начальная вместимость
22+
* @return vector_t* объект вектора
23+
**/
1224
vector_t* vector_create(u32 initial_capacity);
25+
26+
/**
27+
* @brief Добавить элемент в конец вектора
28+
*
29+
* @param vec вектор
30+
* @param item элемент
31+
**/
1332
void vector_push_back(vector_t* vec, void* item);
1433

1534
#endif // DATATYPE_VECTOR_H

src/kernel/kklibc/stdlib.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,85 @@
88
#include "stdlib.h"
99

1010
#include "ctypes.h"
11+
#include "mem.h"
1112
#include "stdio.h"
1213

14+
/**
15+
* @brief Алгоритм Fuzzy search
16+
*
17+
* @param[in] text Текст
18+
* @param[in] query Запрос
19+
* @param[in] build_score Оценка за построение
20+
* @param score Оценка
21+
* @param score_len Длина оценка
22+
*
23+
* @return int Оценка
24+
*/
25+
int fuzzy_search(const char* text, const char* query, int build_score, int** score, u32* score_len) {
26+
u32 total_score = 0;
27+
if (build_score) { // Build score is an optimization when searching through large database
28+
(*score) = (int*)kmalloc(sizeof(int) * strlen(text));
29+
memset(*score, 0, sizeof(int) * strlen(text));
30+
*score_len = strlen(text);
31+
}
32+
33+
u32 first_character_boosts = 1;
34+
35+
for (u32 t_idx = 0; t_idx < strlen(text); t_idx++) {
36+
char t =
37+
tolower(text[t_idx]); // NOTE(deter0): to lower performs kind of strangely probably due to UTF8
38+
for (u32 q_idx = 0; q_idx < strlen(query); q_idx++) {
39+
char q = tolower(query[q_idx]);
40+
41+
if (t == q) {
42+
// Start of word awards more but falls off fast
43+
if (t_idx == 0 || (t_idx > 0 && isspace(text[t_idx - 1]))) {
44+
int factor = 8 / (first_character_boosts++);
45+
46+
if (build_score) {
47+
(*score)[t_idx] += factor;
48+
}
49+
total_score += factor;
50+
} else {
51+
if (build_score) {
52+
(*score)[t_idx]++;
53+
}
54+
total_score++;
55+
}
56+
57+
u32 streak = 0;
58+
for (u32 s_idx = 1; s_idx < strlen(query) - q_idx; s_idx++) {
59+
char sq = tolower(query[q_idx + s_idx]);
60+
char st = tolower(text[t_idx + s_idx]);
61+
62+
if (sq != st) {
63+
break;
64+
}
65+
streak++;
66+
67+
// Beginning of string yields few points more; eg -> "Term" :: "Terminus", "Fluent
68+
// Terminal"
69+
if (((float)t_idx / (float)strlen(text)) <= 0.35) {
70+
streak++;
71+
}
72+
73+
int factor = streak * 3 / (strlen(query) * 0.2);
74+
if (build_score) {
75+
(*score)[t_idx + s_idx] += factor;
76+
}
77+
total_score += factor;
78+
}
79+
80+
// (N * (N + 1) ) /2
81+
// (*score)[t_idx] += (streak * (streak + 1)) / 2;
82+
t_idx += streak;
83+
}
84+
}
85+
}
86+
87+
return total_score;
88+
}
89+
1390
void booltochar(u8 value, u8* str) {
1491
if (value) {
1592
strcpy(str, (u8*)"true");

0 commit comments

Comments
 (0)