From b15016a997f0d3eb2af7ba0120948a3525af35f5 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 18 Mar 2015 14:45:52 +0000 Subject: [PATCH] added String.cpp --- qse/include/qse/cmn/StrBase.hpp | 27 ++++++++++++++- qse/include/qse/cmn/String.hpp | 58 +++++++++++++++++++++++++++++++-- qse/lib/cmn/Makefile.am | 2 +- qse/lib/cmn/Makefile.in | 7 ++-- qse/samples/cmn/str02.cpp | 2 ++ 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/qse/include/qse/cmn/StrBase.hpp b/qse/include/qse/cmn/StrBase.hpp index d13e4e92..c52dd415 100644 --- a/qse/include/qse/cmn/StrBase.hpp +++ b/qse/include/qse/cmn/StrBase.hpp @@ -590,6 +590,31 @@ public: } #endif +#if 0 + int format (const CHAR_TYPE* fmt, va_list ap) + { + int n; + qse_va_list save_ap; + + QSE_VA_COPY (save_ap, ap); + qse_size_t n = this->_opset.format (QSE_NULL, 0, fmt, ap); + if (n == (qse_size_t)-1) + { + // there's conversion error. + return -1; + } + + if (n > this->getCapacity()) this->possess_data (n); + else if (this->_item->isShared()) this->possess_data (); + + QSE_VA_COPY (ap, save_ap); + this->_opset.format (this->_item->buffer, this->_item->capacity + 1, fmt, ap); + + this->_item->size = n; + return 0; + } +#endif + void update (const CHAR_TYPE* str, qse_size_t size) { this->clear (); @@ -714,7 +739,7 @@ public: loop_findIndex: while (i <= max && p[i] != first) i++; if (i > max) return INVALID_INDEX; - + qse_size_t j = i + 1; qse_size_t end = j + size - 1; qse_size_t k = offset + 1; diff --git a/qse/include/qse/cmn/String.hpp b/qse/include/qse/cmn/String.hpp index 03a8d094..b05aa839 100644 --- a/qse/include/qse/cmn/String.hpp +++ b/qse/include/qse/cmn/String.hpp @@ -36,6 +36,7 @@ #include #include #include +#include ///////////////////////////////// QSE_BEGIN_NAMESPACE(QSE) @@ -175,8 +176,60 @@ struct MbStringOpset } }; -typedef StrBase WcString; -typedef StrBase MbString; +// It's a pain to inherit StrBase<>. i do this not to have various va_xxx calls +// in the header file of StrBase. + +class WcString: public StrBase +{ +public: + typedef StrBase ParentType; + + WcString (): ParentType() {} + WcString (Mmgr* mmgr): ParentType(mmgr) {} + WcString (qse_size_t capacity): ParentType(capacity) {} + WcString (Mmgr* mmgr, qse_size_t capacity): ParentType(mmgr, capacity) {} + WcString (const qse_wchar_t* str): ParentType(str) {} + WcString (Mmgr* mmgr, const qse_wchar_t* str): ParentType(mmgr, str) {} + WcString (const qse_wchar_t* str, qse_size_t size): ParentType(str, size) {} + WcString (Mmgr* mmgr, const qse_wchar_t* str, qse_size_t size): ParentType(mmgr, str, size) {} + WcString (qse_wchar_t c, qse_size_t size): ParentType(c, size) {} + WcString (Mmgr* mmgr, qse_wchar_t c, qse_size_t size): ParentType(mmgr, c, size) {} + + WcString (const WcString& str): ParentType(str) {} + + WcString& operator= (const WcString& str) { ParentType::operator=(str); return *this; } + WcString& operator= (const qse_wchar_t* str) { ParentType::operator=(str); return *this; } + WcString& operator= (const qse_wchar_t c) { ParentType::operator=(c); return *this; } + + int format (const qse_wchar_t* fmt, ...); + int formatv (const qse_wchar_t* fmt, va_list ap); +}; + +class MbString: public StrBase +{ +public: + typedef StrBase ParentType; + + MbString (): ParentType() {} + MbString (Mmgr* mmgr): ParentType(mmgr) {} + MbString (qse_size_t capacity): ParentType(capacity) {} + MbString (Mmgr* mmgr, qse_size_t capacity): ParentType(mmgr, capacity) {} + MbString (const qse_mchar_t* str): ParentType(str) {} + MbString (Mmgr* mmgr, const qse_mchar_t* str): ParentType(mmgr, str) {} + MbString (const qse_mchar_t* str, qse_size_t size): ParentType(str, size) {} + MbString (Mmgr* mmgr, const qse_mchar_t* str, qse_size_t size): ParentType(mmgr, str, size) {} + MbString (qse_mchar_t c, qse_size_t size): ParentType(c, size) {} + MbString (Mmgr* mmgr, qse_mchar_t c, qse_size_t size): ParentType(mmgr, c, size) {} + + MbString (const MbString& str): ParentType(str) {} + + MbString& operator= (const MbString& str) { ParentType::operator=(str); return *this; } + MbString& operator= (const qse_mchar_t* str) { ParentType::operator=(str); return *this; } + MbString& operator= (const qse_mchar_t c) { ParentType::operator=(c); return *this; } + + int format (const qse_mchar_t* fmt, ...); + int formatv (const qse_mchar_t* fmt, va_list ap); +}; #if defined(QSE_CHAR_IS_MCHAR) typedef MbString String; @@ -184,7 +237,6 @@ typedef StrBase MbString; typedef WcString String; #endif - ///////////////////////////////// QSE_END_NAMESPACE(QSE) ///////////////////////////////// diff --git a/qse/lib/cmn/Makefile.am b/qse/lib/cmn/Makefile.am index de3ba823..c370c156 100644 --- a/qse/lib/cmn/Makefile.am +++ b/qse/lib/cmn/Makefile.am @@ -141,7 +141,7 @@ if ENABLE_CXX lib_LTLIBRARIES += libqsecmnxx.la libqsecmnxx_la_SOURCES = \ - Mmgr.cpp StdMmgr.cpp HeapMmgr.cpp Mmged.cpp Mpool.cpp + Mmgr.cpp StdMmgr.cpp HeapMmgr.cpp Mmged.cpp Mpool.cpp String.cpp libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined libqsecmnxx_la_LIBADD = diff --git a/qse/lib/cmn/Makefile.in b/qse/lib/cmn/Makefile.in index b54882be..30a72dc3 100644 --- a/qse/lib/cmn/Makefile.in +++ b/qse/lib/cmn/Makefile.in @@ -150,9 +150,9 @@ libqsecmn_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(libqsecmn_la_LDFLAGS) $(LDFLAGS) -o $@ libqsecmnxx_la_DEPENDENCIES = am__libqsecmnxx_la_SOURCES_DIST = Mmgr.cpp StdMmgr.cpp HeapMmgr.cpp \ - Mmged.cpp Mpool.cpp + Mmged.cpp Mpool.cpp String.cpp @ENABLE_CXX_TRUE@am_libqsecmnxx_la_OBJECTS = Mmgr.lo StdMmgr.lo \ -@ENABLE_CXX_TRUE@ HeapMmgr.lo Mmged.lo Mpool.lo +@ENABLE_CXX_TRUE@ HeapMmgr.lo Mmged.lo Mpool.lo String.lo libqsecmnxx_la_OBJECTS = $(am_libqsecmnxx_la_OBJECTS) libqsecmnxx_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \ @@ -435,7 +435,7 @@ libqsecmn_la_SOURCES = alg-base64.c alg-rand.c alg-search.c alg-sort.c \ libqsecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined libqsecmn_la_LIBADD = $(SOCKET_LIBS) $(QUADMATH_LIBS) @ENABLE_CXX_TRUE@libqsecmnxx_la_SOURCES = \ -@ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp HeapMmgr.cpp Mmged.cpp Mpool.cpp +@ENABLE_CXX_TRUE@ Mmgr.cpp StdMmgr.cpp HeapMmgr.cpp Mmged.cpp Mpool.cpp String.cpp @ENABLE_CXX_TRUE@libqsecmnxx_la_LDFLAGS = -version-info 1:0:0 -no-undefined @ENABLE_CXX_TRUE@libqsecmnxx_la_LIBADD = @@ -521,6 +521,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mmgr.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Mpool.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/StdMmgr.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/String.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alg-base64.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alg-rand.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alg-search.Plo@am__quote@ diff --git a/qse/samples/cmn/str02.cpp b/qse/samples/cmn/str02.cpp index 4189e2be..be9f2e3c 100644 --- a/qse/samples/cmn/str02.cpp +++ b/qse/samples/cmn/str02.cpp @@ -88,6 +88,8 @@ void t2() { QSE::MbString x(QSE_MT("this is a string")); qse_printf (QSE_T("x: [%hs] %d %d\n"), x.getBuffer(), (int)x.getCapacity(), (int)x.getLength()); + x.format (QSE_MT("what is this %d %d"), 10, 20); + qse_printf (QSE_T("x: [%hs] %d %d\n"), x.getBuffer(), (int)x.getCapacity(), (int)x.getLength()); } int main ()