[String]stripslashes함수의 구현
stripslashes : addslashes로 quoted된 문자열을 unquoted한다.
gcc ver: 2.95.4
os: linux
ref: php-4.3.0
compile: gcc -o test test.c
require memory free: yes
[code]
#include <stdio.h>
#include <malloc.h>
int stripslashes(char *,char **,int *);
int main(int argc,char **argv){
char *tt="This is sample\\'\\\\\\\"\\\\";
char *ret;
stripslashes(tt,&ret,NULL);
printf("%s %d\n",tt,strlen(tt));
printf("%s %d\n",ret,strlen(ret));
free(ret);
return 0;
}
int stripslashes(char *str,char **ret,int *len){
char *s,*t;
int l;
int str_len=0;
str_len=strlen(str);
if(((*ret)=(char *)malloc(sizeof(char)*str_len+1))==NULL) return 1;
memset((*ret),0x0,sizeof(char)*str_len+1);
memmove((*ret),str,str_len);
if(len!=NULL){
l=*len;
}else{
l=str_len;
}
s=(*ret);
t=(*ret);
while(l>0){
if(*t=='\\'){
t++; /* skip the slash */
if(len!=NULL){
(*len)--;
}
l--;
if(l>0){
if(*t=='0'){
*s++='\0';
t++;
}else{
*s++=*t++; /* preserve the next character */
}
l--;
}
}else{
if(s!=t){
*s++ = *t++;
}else{
s++;
t++;
}
l--;
}
}
if(s!=t){
*s='\0';
}
return 0;
}
[/code]
stripslashes : addslashes로 quoted된 문자열을 unquoted한다.
gcc ver: 2.95.4
os: linux
ref: php-4.3.0
compile: gcc -o test test.c
require memory free: yes
[code]
#include <stdio.h>
#include <malloc.h>
int stripslashes(char *,char **,int *);
int main(int argc,char **argv){
char *tt="This is sample\\'\\\\\\\"\\\\";
char *ret;
stripslashes(tt,&ret,NULL);
printf("%s %d\n",tt,strlen(tt));
printf("%s %d\n",ret,strlen(ret));
free(ret);
return 0;
}
int stripslashes(char *str,char **ret,int *len){
char *s,*t;
int l;
int str_len=0;
str_len=strlen(str);
if(((*ret)=(char *)malloc(sizeof(char)*str_len+1))==NULL) return 1;
memset((*ret),0x0,sizeof(char)*str_len+1);
memmove((*ret),str,str_len);
if(len!=NULL){
l=*len;
}else{
l=str_len;
}
s=(*ret);
t=(*ret);
while(l>0){
if(*t=='\\'){
t++; /* skip the slash */
if(len!=NULL){
(*len)--;
}
l--;
if(l>0){
if(*t=='0'){
*s++='\0';
t++;
}else{
*s++=*t++; /* preserve the next character */
}
l--;
}
}else{
if(s!=t){
*s++ = *t++;
}else{
s++;
t++;
}
l--;
}
}
if(s!=t){
*s='\0';
}
return 0;
}
[/code]
'Native > C++' 카테고리의 다른 글
[String]trim함수의 구현 (0) | 2013.10.02 |
---|---|
[String]explode함수의 구현 (0) | 2013.10.02 |
[String]join함수의 구현 (0) | 2013.10.02 |
[String]str_replace함수의 구현 (0) | 2013.10.02 |
[Array]array_push_str함수의 구현 (0) | 2013.10.02 |