Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Advent of Code related functions #400

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ tests/syntax_error/bad_addressof.jou
tests/wrong_type/assert.jou
tests/wrong_type/cannot_be_indexed.jou
tests/wrong_type/index.jou
stdlib/ascii.jou
40 changes: 40 additions & 0 deletions stdlib/ascii.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "stdlib/str.jou"
import "stdlib/mem.jou"

# Test if a string is ASCII only.
# TODO: test
def is_ascii(s: byte*) -> bool:
for p = s; *p != '\0'; p++:
if *p >= 128:
return False
return True

# Check for '0', '1', ..., '9'.
# TODO: test
def is_ascii_digit(b: byte) -> bool:
return '0' <= b and b <= '9'

# Checks if the given byte is an ASCII punctuation character, such as '*' or '_' or '"'.
# TODO: test
def is_ascii_punctuation(b: byte) -> bool:
return strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", b) != NULL

# Checks if the given byte is an ASCII whitespace character, such as ' ' or '\n'.
# TODO: test
def is_ascii_whitespace(b: byte) -> bool:
return strchr("\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f ", b) != NULL

# Removes ASCII whitespace from both ends of a string in-place.
# Similar to .strip() in Python or .trim() in JavaScript.
def trim_ascii_whitespace(s: byte*) -> void:
start = s
while *start != '\0' and is_ascii_whitespace(*start):
start++

len = strlen(start)
while len > 0 and is_ascii_whitespace(start[len-1]):
len--

if start != s:
memcpy(s, start, len)
s[len] = '\0'
4 changes: 4 additions & 0 deletions stdlib/io.jou
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ declare fprintf(file: FILE *, pattern: byte*, ...) -> int
declare fgetc(file: FILE*) -> int # see getchar()
declare fscanf(file: FILE*, pattern: byte*, ...) -> int

# Reads at most n items of data, each of size "itemsize". Returns number of items read.
# Usually this is used with itemsize 1, so return value is number of bytes.
declare fread(destination: byte*, itemsize: long, n: long, file: FILE*) -> long

# Ensure that output is actually written. It may remain buffered
# if this function isn't called.
declare fflush(file: FILE*) -> int
Expand Down
10 changes: 10 additions & 0 deletions stdlib/str.jou
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ def ends_with(s: byte*, suffix: byte*) -> bool:
return offset >= 0 and strcmp(&s[offset], suffix) == 0

# Return how many bytes at start of s appear in the accept string.
# For example, you can use strspn(s, " ") to get the amount of indentation.
Akuli marked this conversation as resolved.
Show resolved Hide resolved
declare strspn(s: byte*, accept: byte*) -> long

# Return how many bytes at start of s do not appear in the reject string.
# For example, you can use strcspn(s, "\n") to get the length of the first line.
declare strcspn(s: byte*, accept: byte*) -> long

# Copy a string. Assumes it fits. Returned value is dest.
declare strcpy(dest: byte*, source: byte*) -> byte*

Expand All @@ -47,3 +52,8 @@ declare strcat(dest: byte*, source: byte*) -> byte*

# Return a newly allocated (as in malloc()) copy of the string.
declare strdup(s: byte*) -> byte*

# Convert a string to an int.
# Returns 0 on error, and ignores junk at end: atoi("123foo") == 123
declare atoi(s: byte*) -> int
declare atoll(s: byte*) -> long