rewrite the CSVFieldSplite with the template<>

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-27 18:34:15 +08:00
parent 54dc46d1d7
commit b1be1d6fb8
3 changed files with 67 additions and 68 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include<hgl/type/StrChar.h>
namespace hgl namespace hgl
{ {
namespace util namespace util
@ -8,20 +9,80 @@ namespace hgl
* CSV字段拆分工具<br> * CSV字段拆分工具<br>
* tab分隔以及使用引号包裹的字符串 * tab分隔以及使用引号包裹的字符串
*/ */
class CSVFieldSplite template<typename T> class CSVFieldSplite
{ {
const char *str; const T *str;
int str_length; int str_length;
const char *sp; const T *sp;
const char *end; const T *end;
public: 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; ~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<end)
++ep;
*len=ep-sp;
sp=ep+1;
return result;
}
}
};//class CSVFieldSplite };//class CSVFieldSplite
}//namespace util }//namespace util
}//namespace hgl }//namespace hgl

View File

@ -83,7 +83,6 @@ SOURCE_GROUP("Crypt" FILES ${CRYPT_HEADER_FILES} ${CRYPT_SOURCE_FILES})
SET(CSV_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVFieldSplite.h SET(CSV_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVFieldSplite.h
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutput.h ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutput.h
${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutputStream.h ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/csv/CSVOutputStream.h
csv/CSVFieldSplite.cpp
csv/CSVOutputStream.cpp) csv/CSVOutputStream.cpp)
SOURCE_GROUP("CSV" FILES ${CSV_SOURCE}) SOURCE_GROUP("CSV" FILES ${CSV_SOURCE})

View File

@ -1,61 +0,0 @@
#include<hgl/util/csv/CSVFieldSplite.h>
#include<hgl/type/StrChar.h>
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<end)
++ep;
*len=ep-sp;
sp=ep+1;
return result;
}
}
}//namespace util
}//namespace hgl