Skip to content

Commit

Permalink
add: calling parse() from C
Browse files Browse the repository at this point in the history
  • Loading branch information
unw9527 committed Jul 19, 2022
1 parent 8aade02 commit 29ccbfe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ testrun-*
testrun
kind_config
**/.DS_Store
k8s_util/lib/k8sutil.so
k8s_util/lib/k8sutil.so
k8s_util/lib/test
11 changes: 7 additions & 4 deletions k8s_util/k8sutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def canonicalizeQuantity(value):
parse = k8sutil.parse
parse.argtypes = [ctypes.c_char_p]
parse.restype = ctypes.c_void_p
# print(str(value).encode("utf-8"))
parse_output = parse(str(value).encode("utf-8"))
parse_bytes = ctypes.string_at(parse_output)
parse_string = parse_bytes.decode('utf-8')
Expand All @@ -30,8 +31,10 @@ def canonicalizeQuantity(value):
# print(canonicalizeQuantity('.01Ki'))
# assert(float(canonicalizeQuantity('-.484785E-7466')) == float(canonicalizeQuantity('0')))
# print(canonicalizeQuantity('+4678410156.347680E+.6994785'))
print(canonicalizeQuantity("+838612.516637636"))
print(canonicalizeQuantity("838612517m"))
# print(canonicalizeQuantity("+838612.516637636"))
# print(canonicalizeQuantity("838612517m"))
# assert(canonicalizeQuantity("+838612.516637636") == canonicalizeQuantity("838612517m"))
print(canonicalizeQuantity('+099'))
print(canonicalizeQuantity('99'))
# print(canonicalizeQuantity('+099'))
# print(canonicalizeQuantity('99'))
print(canonicalizeQuantity(".2316344e999842"))
# print(canonicalizeQuantity("-92743e6047801799")) # crash
4 changes: 3 additions & 1 deletion k8s_util/lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
k8sutil:
go build -buildmode=c-shared -o k8sutil.so k8sutil.go
gcc test.c -o test ./k8sutil.so

clean:
rm ./k8sutil.so
rm ./k8sutil.so
rm ./test
2 changes: 1 addition & 1 deletion k8s_util/lib/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func parse(valuePtr *C.char) *C.char {

func main() {
// fmt.Println("Hello, world.")
test := "+4678410156.347680E+.6994785"
test := "-92743e6047801799"
ans, err := resource.ParseQuantity(test)
if err != nil {
fmt.Printf("Error in converting the string %v", err)
Expand Down
8 changes: 8 additions & 0 deletions k8s_util/lib/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include "k8sutil.h"

int main(){
char *name = "-92743e6047801799";
printf("The string after parsing is: %s\n", parse(name));
return 0;
}

0 comments on commit 29ccbfe

Please sign in to comment.