1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*
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.
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 <stdlib.h>
#include "istring.h"
char* str_concat(char* ptr, const char* str){
char* str_ptr=(char*)str;
while(*str_ptr!='\0'){
*ptr=*str_ptr;
ptr++;
str_ptr++;
}
*ptr='\0';
return(ptr);
}
int str_concat_memorysafe(char** str_out, int pos, const char* str, int* memory){
char* out_ptr;
if(str_len((char*)str)+pos>=*memory){
*memory=*memory+str_len((char*)str)+pos+1;
resize_str(str_out,*memory);
}
out_ptr=*str_out+pos;
str_concat(out_ptr,str);
return(0);
}
int resize_str(char** out, int memory){
char* tmp_str=calloc(memory,sizeof(char));
char* tmp_ptr=tmp_str;
char* out_ptr=*out;
for(;*out_ptr!='\0';out_ptr++,tmp_ptr++){
*tmp_ptr=*out_ptr;
}
*tmp_ptr='\0';
free(*out);
*out=tmp_str;
return(0);
}
char* str_addchar(char* ptr, const char c){
*ptr=c;
ptr++;
*ptr='\0';
return(ptr);
}
int str_len(char* str){
char* ptr=str;
int ret=0;
while(*ptr!='\0'){ret++;ptr++;}
return(ret);
}
int str_cmp(char* str1, char* str2){
char* ptr1=str1;
char* ptr2=str2;
while(*ptr1==*ptr2 && *ptr1!='\0' && *ptr2!='\0'){
ptr1++;
ptr2++;
}
if(*ptr1=='\0' && *ptr2=='\0'){
return(1);
}
else{
return(0);
}
}
int str_cpy_noalloc(char* input, char* output){
char* ptr;
char* out_ptr;
for(ptr=input,out_ptr=output;*ptr!='\0';ptr++,out_ptr++){
*out_ptr=*ptr;
}
*out_ptr='\0';
return(0);
}
|