added qse_wcsatombsdup()/qse_mbsatowcsdup()

added macro redefinition for QSE_MBLEN_MAX in some special cases
fixed a bug of defining QSE_TOMUPPER and QSE_TOMLOWER wrongly
changed data types of utf8 functions.
fixed null-terminating bugs in qse_mbstowcs()/qse_wcstombs()
This commit is contained in:
2011-12-05 13:43:56 +00:00
parent c2264998f2
commit 2faee1f23f
17 changed files with 535 additions and 295 deletions

View File

@ -91,8 +91,8 @@ typedef qse_ctype_t qse_wctype_t;
#define QSE_ISMSPACE(c) (qse_ismctype(c,QSE_CTYPE_SPACE))
#define QSE_ISMUPPER(c) (qse_ismctype(c,QSE_CTYPE_UPPER))
#define QSE_ISMXDIGIT(c) (qse_ismctype(c,QSE_CTYPE_XDIGIT))
#define QSE_TOMUPPER(c) (qse_ismctype(c,QSE_CTYPE_UPPER))
#define QSE_TOMLOWER(c) (qse_ismctype(c,QSE_CTYPE_LOWER))
#define QSE_TOMUPPER(c) (qse_tomctype(c,QSE_CTYPE_UPPER))
#define QSE_TOMLOWER(c) (qse_tomctype(c,QSE_CTYPE_LOWER))
#define QSE_WCTYPE(name) (qse_getwctype(name))
#define QSE_ISWCTYPE(c,t) (qse_iswctype(c,t))

View File

@ -34,12 +34,6 @@
#if defined(_WIN32) && !defined(__WATCOMC__)
#include <tchar.h>
#define qse_printf _tprintf
#define qse_vprintf _vtprintf
#define qse_fprintf _ftprintf
#define qse_vfprintf _vftprintf
#define qse_fgets(x,y,s) _fgetts(x,y,s)
#define qse_fgetc(x) _fgettc(x)
#define qse_fputs(x,s) _fputts(x,s)
@ -87,12 +81,10 @@ int qse_sprintf (
...
);
#if !defined(_WIN32) || defined(__WATCOMC__)
int qse_vfprintf (QSE_FILE *stream, const qse_char_t* fmt, va_list ap);
int qse_vprintf (const qse_char_t* fmt, va_list ap);
int qse_fprintf (QSE_FILE* file, const qse_char_t* fmt, ...);
int qse_printf (const qse_char_t* fmt, ...);
#endif
int qse_dprintf (const qse_char_t* fmt, ...);
QSE_FILE* qse_fopen (const qse_char_t* path, const qse_char_t* mode);

View File

@ -2360,12 +2360,21 @@ qse_size_t qse_wcsntombsnlen (
/**
* The qse_wcstombs() function converts a null-terminated wide character
* string to a multibyte string and stores it into the buffer pointed to
* by mbs. The pointer to a variable holding the buffer length should be
* by @a mbs. The pointer to a variable holding the buffer length should be
* passed to the function as the third parameter. After conversion, it holds
* the length of the multibyte string excluding the terminating-null character.
* It may not null-terminate the resulting multibyte string if the buffer
* is not large enough. You can check if the resulting mbslen is equal to
* the input mbslen to know it.
* is not large enough.
* @code
* const qse_wchar_t* QSE_T("hello");
* qse_mchar_t mbs[10];
* qse_size_t mbslen = QSE_COUNTOF(mbs);
* n = qse_wcstombs (wcs, mbs, &mbslen);
* if (wcs[n] == QSE_WT('\0') && mbslen < QSE_COUNTOF(mbs))
* {
* // wcs fully scanned and mbs null-terminated
* }
* @endcode
* @return number of wide characters processed
*/
qse_size_t qse_wcstombs (
@ -2428,6 +2437,11 @@ qse_mchar_t* qse_wcstombsdup (
);
qse_wchar_t* qse_mbsatombsdup (
const qse_mchar_t* mbs[],
qse_mmgr_t* mmgr
);
qse_mchar_t* qse_wcsatombsdup (
const qse_wchar_t* wcs[],
qse_mmgr_t* mmgr

View File

@ -24,6 +24,14 @@
#include <qse/types.h>
#include <qse/macros.h>
#if QSE_SIZEOF_WCHAR_T == 2
# define QSE_UTF8LEN_MAX 3
#elif QSE_SIZEOF_WCHAR_T == 4
# define QSE_UTF8LEN_MAX 6
#else
# error Unsupported wide-character size
#endif
#ifdef __cplusplus
extern "C" {
#endif
@ -35,7 +43,7 @@ extern "C" {
* - 0 is returned if @a uc is invalid.
* - A positive integer is returned in all other cases.
*/
int qse_uctoutf8len (
qse_size_t qse_uctoutf8len (
qse_wchar_t uc
);
@ -43,22 +51,31 @@ int qse_uctoutf8len (
* The qse_uctoutf8() function converts a unicode character to a utf8 sequence.
* @return
* - 0 is returned if @a uc is invalid.
* - A negative integer is returned if the utf8 sequence buffer is not
* large enough. It is the negated buffer size required.
* - A positive integer is returned in all other cases.
* - An integer greater than @a size is returned if the utf8 sequence buffer is
* not large enough.
* - An integer between 1 and size inclusive is returned in all other cases.
*/
int qse_uctoutf8 (
qse_size_t qse_uctoutf8 (
qse_wchar_t uc,
qse_mchar_t* utf8,
int size
qse_size_t size
);
int qse_utf8touc (
qse_size_t qse_utf8touc (
const qse_mchar_t* utf8,
int size,
qse_size_t size,
qse_wchar_t* uc
);
qse_size_t qse_utf8len (
const qse_mchar_t* utf8,
qse_size_t len
);
qse_size_t qse_utf8lenmax (
void
);
#ifdef __cplusplus
}
#endif

View File

@ -75,39 +75,6 @@
# define QSE_PRIVATE
#endif
/**
* The #QSE_NULL macro defines a special pointer value to indicate an error or
* that it does not point to anything.
*/
#ifdef __cplusplus
# if QSE_SIZEOF_VOID_P == QSE_SIZEOF_INT
# define QSE_NULL (0)
# elif QSE_SIZEOF_VOID_P == QSE_SIZEOF_LONG
# define QSE_NULL (0l)
# elif QSE_SIZEOF_VOID_P == QSE_SIZEOF_LONG_LONG
# define QSE_NULL (0ll)
# else
# define QSE_NULL (0)
# endif
#else
# define QSE_NULL ((void*)0)
#endif
/**
* The QSE_MCHAR_EOF macro defines an EOF character.
*/
#define QSE_MCHAR_EOF ((qse_mcint_t)-1)
/**
* The QSE_WCHAR_EOF macro defines an EOF character.
*/
#define QSE_WCHAR_EOF ((qse_wcint_t)-1)
/**
* The QSE_CHAR_EOF macro defines an EOF character.
*/
#define QSE_CHAR_EOF ((qse_cint_t)-1)
/**
* The QSE_SIZEOF() macro gets data size in bytes. It is equivalent to the
* sizeof operator. The following code snippet should print sizeof(int)*128.
@ -173,6 +140,39 @@
#define QSE_TYPE_MIN(type) \
((QSE_TYPE_IS_SIGNED(type)? QSE_TYPE_SIGNED_MIN(type): QSE_TYPE_UNSIGNED_MIN(type)))
/**
* The #QSE_NULL macro defines a special pointer value to indicate an error or
* that it does not point to anything.
*/
#ifdef __cplusplus
# if QSE_SIZEOF_VOID_P == QSE_SIZEOF_INT
# define QSE_NULL (0)
# elif QSE_SIZEOF_VOID_P == QSE_SIZEOF_LONG
# define QSE_NULL (0l)
# elif QSE_SIZEOF_VOID_P == QSE_SIZEOF_LONG_LONG
# define QSE_NULL (0ll)
# else
# define QSE_NULL (0)
# endif
#else
# define QSE_NULL ((void*)0)
#endif
/**
* The QSE_MCHAR_EOF macro defines an EOF character.
*/
#define QSE_MCHAR_EOF ((qse_mcint_t)-1)
/**
* The QSE_WCHAR_EOF macro defines an EOF character.
*/
#define QSE_WCHAR_EOF ((qse_wcint_t)-1)
/**
* The QSE_CHAR_EOF macro defines an EOF character.
*/
#define QSE_CHAR_EOF ((qse_cint_t)-1)
/**
* The QSE_BLOCK macro encloses one or more statements in a block with
* no side-effect.

View File

@ -802,4 +802,17 @@ union qse_ubi_t
};
typedef union qse_ubi_t qse_ubi_t;
/*
* Note QSE_MBLEN_MAX was set to 2 when autoconf ran for _WIN32
* with mingw32. This is bad since it can't handle UTF-8. Here are
* some redefinitions. (_WIN64 with mingw64 gave me 5 though).
*/
#if (QSE_SIZEOF_WCHAR_T == 2) && (QSE_MBLEN_MAX < 3)
# undef QSE_MBLEN_MAX
# define QSE_MBLEN_MAX 3
#elif (QSE_SIZEOF_WCHAR_T == 4) && (QSE_MBLEN_MAX < 6)
# undef QSE_MBLEN_MAX
# define QSE_MBLEN_MAX 6
#endif
#endif