diff options
Diffstat (limited to 'src/array.c')
-rw-r--r-- | src/array.c | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/src/array.c b/src/array.c index 11d14f7..7fb6bd8 100644 --- a/src/array.c +++ b/src/array.c @@ -1,5 +1,5 @@ /* -Copyright 2015 Ian Jauslin +Copyright 2015-2022 Ian Jauslin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -72,6 +72,14 @@ int int_array_append(int val, Int_Array* output){ return(0); } +// add a value only if it is not already present +int int_array_append_unique(int val, Int_Array* output){ + if(int_array_find(val,*output)<0){ + int_array_append(val,output); + } + return(0); +} + // concatenate int int_array_concat(Int_Array input, Int_Array* output){ int i; @@ -87,6 +95,15 @@ int int_array_concat(Int_Array input, Int_Array* output){ return(0); } +// concat but only add values that are not already present in the array +int int_array_concat_unique(Int_Array input, Int_Array* output){ + int i; + for(i=0;i<input.length;i++){ + int_array_append_unique(input.values[i],output); + } + return(0); +} + // find (does not assume the array is sorted) int int_array_find(int val, Int_Array array){ int i; @@ -199,7 +216,12 @@ int int_array_print(Int_Array array){ for(i=0;i<array.length-1;i++){ printf("%d,",array.values[i]); } - printf("%d)",array.values[array.length-1]); + if(array.length>0){ + printf("%d)",array.values[array.length-1]); + } + else{ + printf(")"); + } return(0); } @@ -334,6 +356,19 @@ int char_array_concat(Char_Array input, Char_Array* output){ return(0); } +// substring +int char_array_substring(Char_Array str, int begin, int end, Char_Array* substr){ + int i; + if(begin>end || begin<0 || end>=str.length){ + fprintf(stderr,"error: cannot extract a substring [%d,%d] from a string of length %d\n", begin, end, str.length); + exit(-1); + } + init_Char_Array(substr,end-begin); + for(i=begin;i<=end;i++){ + char_array_append(str.str[i],substr); + } + return(0); +} // convert to char* @@ -343,7 +378,7 @@ int char_array_to_str(Char_Array input, char** output){ for(i=0;i<input.length;i++){ (*output)[i]=input.str[i]; } - if((*output)[input.length-1]!='\0'){ + if(input.length==0 || (*output)[input.length-1]!='\0'){ (*output)[input.length]='\0'; } return(0); @@ -371,6 +406,34 @@ int str_to_char_array(char* str, Char_Array* output){ return(0); } +// compare char_array's +int char_array_cmp(Char_Array char_array1, Char_Array char_array2){ + int j; + if(char_array1.length!=char_array2.length){ + return(0); + } + for(j=0;j<char_array1.length && j<char_array2.length;j++){ + if(char_array1.str[j]!=char_array2.str[j]){ + return(0); + } + } + return(1); +} + +// compare a char_array and a char* +int char_array_cmp_str(Char_Array char_array, char* str){ + int j; + for(j=0;j<char_array.length && str[j]!='\0';j++){ + if(char_array.str[j]!=str[j]){ + return(0); + } + } + if(j==char_array.length && str[j]=='\0'){ + return(1); + } + return(0); +} + // format strings int char_array_snprintf(Char_Array* output, char* fmt, ...){ |