From dd9bd74c83e285df190fff2d85213c2576e459fb Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 3 Nov 2023 12:25:02 -0400 Subject: [PATCH] dynamic strings --- src/dstring.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/dstring.h | 76 ++++++++++++++++ 2 files changed, 319 insertions(+) create mode 100644 src/dstring.c create mode 100644 src/dstring.h diff --git a/src/dstring.c b/src/dstring.c new file mode 100644 index 0000000..eb64cfb --- /dev/null +++ b/src/dstring.c @@ -0,0 +1,243 @@ +/* +Copyright 2017-2023 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "dstring.h" +#include +#include + +// init +int dstring_init (dstring* str, unsigned int memory){ + str->string=calloc(memory,sizeof(char)); + str->memory=memory; + str->length=0; + return(0); +} +int dstring_free (dstring str){ + free(str.string); + return(0); +} + +// resize memory +int dstring_resize (dstring* str, unsigned int newsize){ + unsigned int i; + dstring new_str; + dstring_init (&new_str, newsize); + for(i=0;ilength;i++){ + new_str.string[i]=str->string[i]; + } + new_str.length=str->length; + free(str->string); + *str=new_str; + return(0); +} + +// add a character +int dstring_append (char val, dstring* output){ + if(output->length >= output->memory){ + dstring_resize (output,2*output->memory+1); + } + output->string[output->length]=val; + output->length++; + return(0); +} + +// copy +int dstring_cpy (dstring input, dstring* output){ + dstring_init (output, input.length); + dstring_cpy_noinit (input, output); + return(0); +} +// do not init output str +int dstring_cpy_noinit (dstring input, dstring* output){ + unsigned int i; + if(output->memorymemory); + return(-1); + } + for(i=0;istring[i]=input.string[i]; + } + output->length=input.length; + return(0); +} + + +// concatenate +int dstring_concat (dstring input, dstring* output){ + unsigned int i; + for(i=0;iend || end>=str.length){ + fprintf(stderr,"error: cannot take substring [%u,%u] of dstring of lengdth %u\n",begin,end,str.length); + return(-1); + } + dstring_init (substr,end-begin); + for(i=begin;i<=end;i++){ + dstring_append (str.string[i], substr); + } + return(0); +} + +// find +int dstring_find (char val, dstring str){ + unsigned int i; + for(i=0;istr2.length){ + return(1); + } + + // compare terms + for(i=0;istring[input->length-1]!='\0'){ + if(input->length==input->memory){ + dstring_resize(input,input->length+1); + } + // add final '\0' + input->string[input->length]='\0'; + } + return(input->string); +} + +// convert from char* +int str_to_dstring(char* str, dstring* output){ + char* ptr; + unsigned int str_len=0; + for(ptr=str;*ptr!='\0';ptr++){ + str_len++; + } + dstring_init(output, str_len); + for(ptr=str;*ptr!='\0';ptr++){ + dstring_append(*ptr,output); + } + return(0); +} + +// compare a dstring and a char* +int dstring_cmp_str(dstring dstring, char* str){ + unsigned int j; + for(j=0;jsize){ + // resize + free(out_str); + // +1 for '\0' + size=extra_size+1; + out_str=calloc(size,sizeof(char)); + // read format again + va_start(vaptr, fmt); + vsnprintf(out_str,size,fmt,vaptr); + va_end(vaptr); + } + + // write to char array + for(ptr=out_str;*ptr!='\0';ptr++){ + dstring_append(*ptr, output); + } + + free(out_str); + + return(0); +} diff --git a/src/dstring.h b/src/dstring.h new file mode 100644 index 0000000..59f27bb --- /dev/null +++ b/src/dstring.h @@ -0,0 +1,76 @@ +/* +Copyright 2017-2023 Ian Jauslin + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef DSTRING_H +#define DSTRING_H + +#include + +typedef struct dstring { + char* string; + unsigned int length; + unsigned int memory; +} dstring; + +// init +int dstring_init (dstring* str, unsigned int memory); +int dstring_free (dstring str); + +// resize memory +int dstring_resize (dstring* str, unsigned int newsize); + +// add a character +int dstring_append (char val, dstring* output); + +// copy +int dstring_cpy (dstring input, dstring* output); +// do not init output str +int dstring_cpy_noinit (dstring input, dstring* output); + + +// concatenate +int dstring_concat (dstring input, dstring* output); + +// sub-str +int dstring_substr (dstring str, unsigned int begin, unsigned int end, dstring* substr); + +// find +int dstring_find (char val, dstring str); + +// compare strings +int dstring_cmp (dstring str1, dstring str2); + +// print str +int dstring_print (dstring str, FILE* file); + +// append a char* +int dstring_append_str(char* str, dstring* output); + +// convert to char* +int dstring_to_str(dstring input, char** output); +// noinit (changes the size of input if needed) +char* dstring_to_str_noinit(dstring* input); + +// convert from char* +int str_to_dstring(char* str, dstring* output); + +// compare a dstring and a char* +int dstring_cmp_str(dstring dstring, char* str); + +// format strings +int dstring_snprintf(dstring* output, char* fmt, ...); + +#endif