Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
travisdoor committed Sep 13, 2020
1 parent bd028b8 commit a22f991
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 40 deletions.
30 changes: 17 additions & 13 deletions _gendocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,49 @@
WDIR=$(pwd)
echo Running in ${WDIR}
mkdir -p docs
mkdir -p docs/API
mkdir -p docs/api

# build
cd lib/bl/api/build
echo
echo Process $(pwd)
bdg *.bl
mkdir -p ${WDIR}/docs/API/build
mv *.md ${WDIR}/docs/API/build
mkdir -p ${WDIR}/docs/api/build
rm ${WDIR}/docs/api/build/*.md
mv *.md ${WDIR}/docs/api/build
cd ${WDIR}

# STD
cd lib/bl/api/std
echo
echo Process $(pwd)
bdg *.bl
mkdir -p ${WDIR}/docs/API/std
mv *.md ${WDIR}/docs/API/std
mkdir -p ${WDIR}/docs/api/std
rm ${WDIR}/docs/api/std/*.md
mv *.md ${WDIR}/docs/api/std
cd ${WDIR}

# Builtin
cd lib/bl/api
echo
echo Process $(pwd)
bdg *.bl
mkdir -p ${WDIR}/docs/API/builtin
mv *.md ${WDIR}/docs/API/builtin
mkdir -p ${WDIR}/docs/api/builtin
rm ${WDIR}/docs/api/builtin/*.md
mv *.md ${WDIR}/docs/api/builtin
cd ${WDIR}

# OS
cd lib/bl/api/os
echo
echo Process $(pwd)
bdg docs.txt
mkdir -p ${WDIR}/docs/API/os
mv *.md ${WDIR}/docs/API/os
mkdir -p ${WDIR}/docs/api/os
rm ${WDIR}/docs/api/os/*.md
mv *.md ${WDIR}/docs/api/os
cd ${WDIR}

# Examples
# examples
cd examples
echo
echo Process $(pwd)
Expand All @@ -50,14 +54,14 @@ for f in *.bl
do
echo "Processing $f file..."
OUT_FILE="_tmp/$f.md"
#echo "# $f" >> $OUT_FILE
echo "#" ${f#*_} >> $OUT_FILE
echo '```c' >> $OUT_FILE
cat $f >> $OUT_FILE
echo '```' >> $OUT_FILE
cat $WDIR/_disqus.html >> $OUT_FILE
done

mkdir -p ${WDIR}/docs/Examples
mv _tmp/*.md ${WDIR}/docs/Examples
mkdir -p ${WDIR}/docs/examples
mv _tmp/*.md ${WDIR}/docs/examples
rm -r -f _tmp
cd ${WDIR}
21 changes: 21 additions & 0 deletions _ideas.bl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,24 @@ memcpy :: fn (dest: *u8, src: *u8, len: s64) void #intrinsic "memcpy";
// comtime function call
#comptime foo();




// Function tags
my_command :: fn (buf: *Buffer, args: ...string) bool
#tags COMMAND
#meta "This is my cool command!"
{
return true;
}

main :: fn () s32 {
fns :: tagged_functions(); // []Any
loop i := 0; i < fns.len; i += 1 {
f :: fns[i];
if f.type_info.tag & COMMAND {
print("%\n", f.type_info.meta)
}
}
return 0;
}
220 changes: 212 additions & 8 deletions lib/bl/api/std/string.bl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ string_empty :: {:string: 0};

/*!
# string_new
Initialize new dynamic string. Created string is guaranteed to be zero terminated.
Overloaded function creating new dynamic string instance. Created string is
guaranteed to be zero terminated.

!!! WARNING
Every new string must be deleted by `string_delete` call.
Expand Down Expand Up @@ -534,10 +535,80 @@ string_split_by_first :: fn (str: string, delimiter: u8, lhs: *string, rhs: *str
return true;
}

// @DOC
/*!
# string_insert
Overloaded function inserting one character or other string at desired position.

## Declaration
```c
fn (str: *string, index: s32, v: u8) bool #inline
```
## Description
Insert one character into `str` at `index` and return `true` when character was inserted.

!!! NOTE
The input `str` can be reallocated when `index` is equal to input string `len` and more
space is needed.

## Arguments
* `str`
Valid pointer to input string.
* `index`
Position in the string, could be equal to input string `len`.
* `v`
Character to be inserted.

## Result
Returns `true` when character was inserted.

---

## Declaration
```c
fn (str: *string, index: s32, v: string) bool
```
## Description
Insert other string into `str` at `index` and return `true` when string was inserted.

!!! NOTE
The input `str` can be reallocated when `index` is equal to input string `len` and
more space is needed.

!!! NOTE
Function does nothing (return `false`) when `v` string is empty.

## Arguments
* `str`
Valid pointer to input string.
* `index`
Position in the string, could be equal to input string `len`.
* `v`
String to be inserted.

## Result
Returns `true` when string was inserted.
*/
string_insert :: fn { _string_insert; u8_insert; };

// @DOC
/*!
# string_erase
## Declaration
```c
string_erase :: fn (str: *string, index: s32) bool
```
## Description
Erase one character at `index` position and return true when character
was erased. The '`index` value is checked to fit in string bounds.

## Arguments
* `str`
Pointer to string.
* `index`
Index position in string to be erased.

## Result
True when index points to valid position in string.
*/
string_erase :: fn (str: *string, index: s32) bool {
if str.len == 0 { return false; }
if index >= str.len { return false; }
Expand All @@ -550,7 +621,31 @@ string_erase :: fn (str: *string, index: s32) bool {
return true;
}

// @DOC
/*!
# string_split_by
## Declaration
```c
string_split_by :: fn (str: string, delimiter: u8) []string
```
## Description
Split the `str` input string by delimiter and return new slice containing
all found sub-strings.

!!! WARNING
String slice should be terminated by `slice_terminate` call.

!!! NOTE
Slice elements just points into original string and should not be terminated.

## Arguments
* `str`
String.
* `delimiter`
Delimiter character.

## Result
String slice.
*/
string_split_by :: fn (str: string, delimiter: u8) []string {
count :: string_count(str, delimiter) + 1;
ret: []string;
Expand All @@ -568,7 +663,24 @@ string_split_by :: fn (str: string, delimiter: u8) []string {
return ret;
}

// @DOC
/*!
# string_count
## Declaration
```c
string_count :: fn (str: string, c: u8) s32
```
## Description
Counts desired character occurrence in the input string.

## Arguments
* `str`
Input string.
* `c`
Character.

## Result
Count of character occurrence.
*/
string_count :: fn (str: string, c: u8) s32 {
count := 0;
loop i := 0; i < str.len; i += 1 {
Expand All @@ -577,15 +689,39 @@ string_count :: fn (str: string, c: u8) s32 {
return count;
}

// @DOC
/*!
# string_to_lower
## Declaration
```c
string_to_lower :: fn (str: string)
```
## Description
Converts input string to lower case.

## Arguments
* `str`
Input string.
*/
string_to_lower :: fn (str: string) {
c_tolower :: fn (c: s32) s32 #extern "tolower";
loop i := 0; i < str.len; i += 1 {
str[i] = auto c_tolower(auto str[i]);
}
}

// @DOC
/*!
# string_to_upper
## Declaration
```c
string_to_upper :: fn (str: string)
```
## Description
Converts input string to upper case.

## Arguments
* `str`
Input string.
*/
string_to_upper :: fn (str: string) {
c_toupper :: fn (c: s32) s32 #extern "toupper";
loop i := 0; i < str.len; i += 1 {
Expand Down Expand Up @@ -630,7 +766,22 @@ string_replace_all :: fn (str: *string, c: u8, with := '\0') s32 {
return replaced;
}

// @DOC
/*!
# string_hash
## Declaration
```c
string_hash :: fn (str: string) u32
```
## Description
Calculates string `u32` hash.

## Arguments
* `str`
Input string.

## Result
String hash.
*/
string_hash :: fn (str: string) u32 {
hash : u32 = 5381;
loop i := 0; i < str.len; i += 1 {
Expand All @@ -640,8 +791,60 @@ string_hash :: fn (str: string) u32 {
return hash;
}

/*!
# string_is_null
## Declaration
```c
string_is_null :: fn (s: string) bool #inline
```
## Description
Helper inline function returning `true` when string is null. In such case string
`len` cound be any value.

## Arguments
* `str`
Input string.

## Result
True when string `ptr` is null.
*/
string_is_null :: fn (s: string) bool #inline { return s.ptr == null; }

/*!
# string_is_empty
## Declaration
```c
string_is_empty :: fn (s: string) bool #inline
```
## Description
Helper inline function returning `true` when string is empty. In such case string
`ptr` cound be any pointer.

## Arguments
* `str`
Input string.

## Result
True when string `len` is 0.
*/
string_is_empty :: fn (s: string) bool #inline { return s.len == 0; }

/*!
# string_is_null_or_empty
## Declaration
```c
string_is_null_or_empty :: fn (s: string) bool #inline
```
## Description
Helper inline function returning `true` when string is empty and null.

## Arguments
* `str`
Input string.

## Result
True when string `len` is 0 and `ptr` is null.
*/
string_is_null_or_empty :: fn (s: string) bool #inline { return s.ptr == null || s.len == 0; }

// Some legacy C string libc apis.
Expand Down Expand Up @@ -780,6 +983,7 @@ u8_insert :: fn (str: *string, index: s32, v: u8) bool #inline {
}

_string_insert :: fn (str: *string, index: s32, v: string) bool {
if !str { panic("Input string is null!"); }
if v.len == 0 { return false; }
if index > str.len { return false; }
if index == str.len { // appending
Expand Down
Loading

0 comments on commit a22f991

Please sign in to comment.