diff --git a/inc/hgl/util/csv/CSVFieldSplite.h b/inc/hgl/util/csv/CSVFieldSplite.h index 0a2e04d..c6f89bd 100644 --- a/inc/hgl/util/csv/CSVFieldSplite.h +++ b/inc/hgl/util/csv/CSVFieldSplite.h @@ -1,5 +1,6 @@ #pragma once +#include namespace hgl { namespace util @@ -8,20 +9,80 @@ namespace hgl * CSV字段拆分工具
* 支持逗号分隔与tab分隔以及使用引号包裹的字符串 */ - class CSVFieldSplite + template class CSVFieldSplite { - const char *str; + const T *str; int str_length; - const char *sp; - const char *end; + const T *sp; + const T *end; public: - CSVFieldSplite(const char *s,const int length); + CSVFieldSplite() + { + str=nullptr; + str_length=0; + sp=nullptr; + end=nullptr; + } + + CSVFieldSplite(const T *s,const int length){Start(s,length);} ~CSVFieldSplite()=default; - const char *next_field(int *len); + void Start(const T *s,const int length) + { + str=s; + str_length=length; + sp=str; + end=str+str_length; + } + + const T *next_field(int *len) + { + if(!len)return(nullptr); + if(sp>=end)return(nullptr); + + if(*sp==','||*sp=='\t') + { + *len=0; + ++sp; + return sp; + } + + const T *result; + + if(*sp=='"') + { + ++sp; + + const T *ep=hgl::strchr(sp,T('"')); + + if(!ep) + return nullptr; + + result=sp; + + *len=ep-sp; + sp=ep+2; + return result; + } + else + { + result=sp; + + const T *ep=sp+1; + + while(*ep!=','&&*ep!='\t'&&ep -#include - -namespace hgl -{ - namespace util - { - CSVFieldSplite::CSVFieldSplite(const char *s,const int length) - { - str=s; - str_length=length; - sp=str; - end=str+str_length; - } - - const char *CSVFieldSplite::next_field(int *len) - { - if(!len)return(nullptr); - if(sp>=end)return(nullptr); - - if(*sp==','||*sp=='\t') - { - *len=0; - ++sp; - return sp; - } - - const char *result; - - if(*sp=='"') - { - ++sp; - - const char *ep=hgl::strchr(sp,'"'); - - if(!ep) - return nullptr; - - result=sp; - - *len=ep-sp; - sp=ep+2; - return result; - } - else - { - result=sp; - - const char *ep=sp+1; - - while(*ep!=','&&*ep!='\t'&&ep