Author:ptrace
Comitter:ptrace
Date:2026-01-20 04:38:33 UTC
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..805c007
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
# Jai
.build
/bin
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..63e85fc
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
MIT License
Copyright (c) 2026 dev@ptrace.dev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..52b0fcc
--- /dev/null
+++ b/README.md
@@ -0,0 +1,40 @@
# Stringpad
When you do this:
```
#import "stringpad";
main :: () {
foo := "FooBar";
padding := 20;
char := ".";
t := string_pad_left(foo, padding, char);
r := string_pad_right(foo, padding, char);
z := string_pad_lr(foo, padding, char);
log("%\n%\n%", t, r, z);
}
```
It does this:
```
..............FooBar
FooBar..............
.......FooBar.......
```
Don't forget to free memory.
## Compiler
You need this version:
```
$ jai -version
Version: beta 0.2.024, built on 31 December 2025.
```
## Example
Run with: `jai build.jai - run silent`
diff --git a/build.jai b/build.jai
new file mode 100644
index 0000000..8036650
--- /dev/null
+++ b/build.jai
@@ -0,0 +1,142 @@
#import "Basic";
#import "Compiler";
#import "File";
#import "Autorun";
build :: () {
args := get_build_options().compile_time_command_line;
// args
args_help := array_find(args, "help");
args_compiler_silent := array_find(args, "silent");
args_program_run := array_find(args, "run");
args_build_release := array_find(args, "release");
args_memory_debug := array_find(args, "memory");
// program args
program_args := program_args_collect(args);
defer array_free(program_args);
// -----------------------------------------
w := compiler_create_workspace();
if !w {
log("Workspace creation failed.");
return;
}
set_build_options_dc(.{ do_output = false });
if args_help {
args_help_print();
return;
}
print("The workspace w is %\n", w);
make_directory_if_it_does_not_exist("bin");
target_options := get_build_options(w);
target_options.output_executable_name = "program";
target_options.output_path = "bin";
import_path: [..] string;
array_add(*import_path, ..target_options.import_path);
array_add(*import_path, ".");
target_options.import_path = import_path;
if args_compiler_silent target_options.text_output_flags = 0;
if args_build_release {
build_release(w, *target_options);
} else {
build_debug(w, *target_options);
}
compiler_begin_intercept(w);
add_build_file(tprint("%test/test.jai", #filepath), w);
add_build_string(tprint("MEMORY_DEBUGGER_ENABLED :: %;", args_memory_debug), w);
compiler_response := message_loop();
compiler_end_intercept(w);
if !compiler_response {
log("Compiler response failed.");
return;
}
if args_program_run run_build_result(w, program_args);
}
build_debug :: (w: Workspace, target_options: *Build_Options) {
log("Choosing debug options...");
target_options.backend =.X64;
set_optimization(target_options, Optimization_Type.DEBUG, true);
set_build_options(target_options.*, w);
}
build_release :: (w: Workspace, target_options: *Build_Options) {
log("Choosing release options...");
target_options.backend = .LLVM;
set_optimization(target_options, Optimization_Type.VERY_OPTIMIZED);
set_build_options(target_options.*, w);
}
args_help_print :: () {
help_message := #string _END_
Usage: jai build.jai - [OPTIONS] :: [PROGRAM ARGS]
Options:
help Prints this help menu.
silent Disables compiler/linker statistics.
run Runs your program afterwards.
release Builds with release options. If omitted, it builds a debug build.
memory Enables the memory leak detector.
Passing Args to your Program:
If you want to supply args to your program, pass it like that:
`jai build.jai - run :: my_arg1 foo bar abc ABC`
Everything after the `::` get's forwarded to your program.
_END_;
log(help_message);
}
program_args_collect :: (args: []string, divider: string = "::") -> [..]string {
buf: [..]string;
success, match := array_find(args, divider);
if success for i: match+1..args.count-1 array_add(*buf, args[i]);
return buf;
}
message_loop :: () -> success: bool {
while true {
message := compiler_wait_for_message();
if !message break;
if message.kind == {
case .COMPLETE;
message_complete := cast(*Message_Complete) message;
return message_complete.error_code == 0;
}
}
return false;
}
#placeholder MEMORY_DEBUGGER_ENABLED;
main :: () {}
#run build();
diff --git a/stringpad/lib.jai b/stringpad/lib.jai
new file mode 100644
index 0000000..db48b1d
--- /dev/null
+++ b/stringpad/lib.jai
@@ -0,0 +1,82 @@
/*
* MIT License
*
* Copyright (c) 2026 dev@ptrace.dev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#import "Basic";
#import "Math";
string_pad_left :: (s: string, padding: int, char: string = " ") -> string {
code :: #string DONE
for 1..length {
append(*sb, `char);
}
append(*sb, `s);
DONE;
__string_pad(code);
}
string_pad_right :: (s: string, padding: int, char: string = " ") -> string {
code :: #string DONE
append(*sb, `s);
for 1..length {
append(*sb, `char);
}
DONE;
__string_pad(code);
}
string_pad_lr :: (s: string, padding: int, char: string = " ") -> string {
code :: #string DONE
half: int = xx floor(xx length / 2.0);
for 1..half {
append(*sb, `char);
}
append(*sb, `s);
for 1..half {
append(*sb, `char);
}
DONE;
__string_pad(code);
}
#scope_file
__string_pad :: ($code: string) #expand {
sb: String_Builder;
init_string_builder(*sb);
length := `padding - `s.count;
#insert code;
`return builder_to_string(*sb);
}
diff --git a/stringpad/module.jai b/stringpad/module.jai
new file mode 100644
index 0000000..60423ff
--- /dev/null
+++ b/stringpad/module.jai
@@ -0,0 +1 @@
#load "lib.jai";
diff --git a/test/test.jai b/test/test.jai
new file mode 100644
index 0000000..d0a84a6
--- /dev/null
+++ b/test/test.jai
@@ -0,0 +1,20 @@
#import "Basic"()(
MEMORY_DEBUGGER = MEMORY_DEBUGGER_ENABLED
);
#import "stringpad";
main :: () {
#if MEMORY_DEBUGGER_ENABLED defer report_memory_leaks();
foo := "FooBar";
padding := 20;
char := ".";
t := string_pad_left(foo, padding, char);
r := string_pad_right(foo, padding, char);
z := string_pad_lr(foo, padding, char);
log("%\n%\n%", t, r, z);
}