QoreEncoding.h
Go to the documentation of this file.
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreEncoding.h
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef _QORE_ENCODING_H
#define _QORE_ENCODING_H
#include <qore/common.h>
#include <qore/QoreThreadLock.h>
#include <cstring>
#include <map>
#include <string>
#include <strings.h>
typedef size_t (*mbcs_length_t)(const char* str, const char* end, bool &invalid);
typedef size_t (*mbcs_end_t)(const char* str, const char* end, size_t num_chars, bool &invalid);
typedef size_t (*mbcs_pos_t)(const char* str, const char* ptr, bool &invalid);
typedef qore_offset_t (*mbcs_charlen_t)(const char* str, size_t valid_len);
typedef unsigned (*mbcs_get_unicode_t)(const char* p);
// private implementation of the QoreEncoding class
struct qore_encoding_private;
class QoreEncoding {
friend struct qore_encoding_private;
protected:
qore_encoding_private* priv;
public:
DLLLOCAL QoreEncoding(const char* code, const char* desc = nullptr, unsigned char minwidth = 1,
unsigned char maxwidth = 1, mbcs_length_t l = nullptr, mbcs_end_t e = nullptr, mbcs_pos_t p = nullptr,
mbcs_charlen_t c = nullptr, mbcs_get_unicode_t gu = nullptr, bool ascii_compat = true);
DLLLOCAL ~QoreEncoding();
DLLEXPORT size_t getLength(const char* p, const char* end, bool& invalid) const;
DLLEXPORT size_t getLength(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT size_t getByteLen(const char* p, const char* end, size_t c, bool& invalid) const;
DLLEXPORT size_t getByteLen(const char* p, const char* end, size_t c, ExceptionSink* xsink) const;
DLLEXPORT size_t getCharPos(const char* p, const char* end, bool& invalid) const;
DLLEXPORT size_t getCharPos(const char* p, const char* end, ExceptionSink* xsink) const;
DLLEXPORT qore_offset_t getCharLen(const char* p, size_t valid_len) const;
DLLEXPORT bool isMultiByte() const;
DLLEXPORT const char* getCode() const;
DLLEXPORT const char* getDesc() const;
DLLEXPORT int getMaxCharWidth() const;
DLLEXPORT unsigned getMinCharWidth() const;
DLLEXPORT bool isAsciiCompat() const;
DLLEXPORT int getUnicode(const char* p, const char* end, unsigned& clen, ExceptionSink* xsink) const;
};
// case-insensitive maps for encodings
typedef std::map<const char*, QoreEncoding*, ltcstrcase> encoding_map_t;
typedef std::map<const char*, const QoreEncoding*, ltcstrcase> const_encoding_map_t;
class QoreString;
class QoreEncodingManager {
private:
DLLLOCAL static encoding_map_t emap;
DLLLOCAL static const_encoding_map_t amap;
DLLLOCAL static QoreThreadLock mutex;
DLLLOCAL static const QoreEncoding* addUnlocked(const char* n_code, const char* n_desc = 0, unsigned char n_minwidth = 1, unsigned char n_maxwidth = 1, mbcs_length_t l = 0, mbcs_end_t e = 0, mbcs_pos_t p = 0, mbcs_charlen_t c = 0, mbcs_get_unicode_t gu = 0, bool n_ascii_compat = true);
DLLLOCAL static const QoreEncoding* findUnlocked(const char* name);
public:
DLLEXPORT static void addAlias(const QoreEncoding* qcs, const char* alias);
DLLEXPORT static const QoreEncoding* findCreate(const char* name);
DLLEXPORT static const QoreEncoding* findCreate(const QoreString* str);
DLLEXPORT static void showEncodings();
DLLEXPORT static void showAliases();
DLLEXPORT static const QoreEncoding* add(const char* code, const char* desc = 0, unsigned char maxwidth = 1, mbcs_length_t l = 0, mbcs_end_t e = 0, mbcs_pos_t p = 0, mbcs_charlen_t = 0);
DLLLOCAL static void init(const char* def);
DLLLOCAL QoreEncodingManager();
DLLLOCAL ~QoreEncodingManager();
};
DLLEXPORT size_t q_get_byte_len(const QoreEncoding* enc, const char* p, const char* end, size_t c, ExceptionSink* xsink);
DLLEXPORT qore_offset_t q_get_char_len(const QoreEncoding* enc, const char* p, size_t valid_len, ExceptionSink* xsink);
DLLEXPORT extern QoreEncodingManager QEM;
// builtin character encodings
DLLEXPORT extern const QoreEncoding* QCS_DEFAULT,
*QCS_USASCII,
*QCS_UTF8,
*QCS_UTF16,
*QCS_UTF16BE,
*QCS_UTF16LE,
*QCS_ISO_8859_1,
*QCS_ISO_8859_2,
*QCS_ISO_8859_3,
*QCS_ISO_8859_4,
*QCS_ISO_8859_5,
*QCS_ISO_8859_6,
*QCS_ISO_8859_7,
*QCS_ISO_8859_8,
*QCS_ISO_8859_9,
*QCS_ISO_8859_10,
*QCS_ISO_8859_11,
*QCS_ISO_8859_13,
*QCS_ISO_8859_14,
*QCS_ISO_8859_15,
*QCS_ISO_8859_16,
*QCS_KOI8_R,
*QCS_KOI8_U,
*QCS_KOI7;
#endif // _QORE_ENCODING_H
mbcs_charlen_t
qore_offset_t(* mbcs_charlen_t)(const char *str, size_t valid_len)
for multi-byte encodings: gives the number of total bytes for the character given one or more charact...
Definition: QoreEncoding.h:63
QCS_UTF8
DLLEXPORT const QoreEncoding * QCS_UTF8
UTF-8 multi-byte encoding (only UTF-8 and UTF-16 are multi-byte encodings)
mbcs_end_t
size_t(* mbcs_end_t)(const char *str, const char *end, size_t num_chars, bool &invalid)
for multi-byte character set encodings: gives the number of bytes for the number of chars ...
Definition: QoreEncoding.h:53
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
QoreEncoding::isMultiByte
DLLEXPORT bool isMultiByte() const
returns true if the encoding is a multi-byte encoding
QCS_ISO_8859_8
DLLEXPORT const QoreEncoding * QCS_ISO_8859_8
Hebrew character set.
QCS_ISO_8859_1
DLLEXPORT const QoreEncoding * QCS_ISO_8859_1
latin-1, Western European encoding
QoreEncoding::isAsciiCompat
DLLEXPORT bool isAsciiCompat() const
returns true if the character encoding is backwards-compatible with ASCII
QCS_UTF16
DLLEXPORT const QoreEncoding * QCS_UTF16
UTF-16 (only UTF-8 and UTF-16* are multi-byte encodings)
QCS_DEFAULT
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
QoreEncoding::getByteLen
DLLEXPORT size_t getByteLen(const char *p, const char *end, size_t c, bool &invalid) const
gives the number of bytes for the number of chars in the string or up to the end of the string ...
QoreEncodingManager::addAlias
static DLLEXPORT void addAlias(const QoreEncoding *qcs, const char *alias)
adds an alias for an encoding
QCS_ISO_8859_2
DLLEXPORT const QoreEncoding * QCS_ISO_8859_2
latin-2, Central European encoding
QCS_ISO_8859_11
DLLEXPORT const QoreEncoding * QCS_ISO_8859_11
Thai character set.
QoreEncodingManager
manages encodings in Qore
Definition: QoreEncoding.h:205
QCS_ISO_8859_3
DLLEXPORT const QoreEncoding * QCS_ISO_8859_3
latin-3, Southern European character set
mbcs_length_t
size_t(* mbcs_length_t)(const char *str, const char *end, bool &invalid)
for multi-byte character set encodings: gives the length of the string in characters ...
Definition: QoreEncoding.h:50
QCS_ISO_8859_4
DLLEXPORT const QoreEncoding * QCS_ISO_8859_4
latin-4, Northern European character set
QoreEncodingManager::showEncodings
static DLLEXPORT void showEncodings()
prints out all valid encodings to stdout
QCS_USASCII
DLLEXPORT const QoreEncoding * QCS_USASCII
ascii encoding
mbcs_get_unicode_t
unsigned(* mbcs_get_unicode_t)(const char *p)
returns the unicode code point for the given character, assumes there is enough data for the characte...
Definition: QoreEncoding.h:66
QCS_ISO_8859_10
DLLEXPORT const QoreEncoding * QCS_ISO_8859_10
latin-6, Nordic character set
QoreString
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:93
QoreEncoding::getDesc
DLLEXPORT const char * getDesc() const
returns the description for the encoding
QCS_KOI8_U
DLLEXPORT const QoreEncoding * QCS_KOI8_U
Ukrainian: Kod Obmena Informatsiey, 8 bit.
QEM
DLLEXPORT QoreEncodingManager QEM
the QoreEncodingManager object
QoreEncoding::getMinCharWidth
DLLEXPORT unsigned getMinCharWidth() const
returns the minimum character width in bytes for the encoding
QCS_ISO_8859_9
DLLEXPORT const QoreEncoding * QCS_ISO_8859_9
latin-5, Turkish character set
QoreEncodingManager::showAliases
static DLLEXPORT void showAliases()
prints out all aliases to stdout
QoreEncodingManager::findCreate
static DLLEXPORT const QoreEncoding * findCreate(const char *name)
finds an encoding if it exists (also looks up against alias names) and creates a new one if it doesn&#39;...
QoreEncoding::getCode
DLLEXPORT const char * getCode() const
returns the string code (ex: "UTF-8") for the encoding
QoreEncoding::getMaxCharWidth
DLLEXPORT int getMaxCharWidth() const
returns the maximum character width in bytes for the encoding
QoreEncoding::getLength
DLLEXPORT size_t getLength(const char *p, const char *end, bool &invalid) const
gives the length of the string in characters
QCS_UTF16BE
DLLEXPORT const QoreEncoding * QCS_UTF16BE
UTF-16BE (only UTF-8 and UTF-16* are multi-byte encodings)
QCS_KOI7
DLLEXPORT const QoreEncoding * QCS_KOI7
Russian: Kod Obmena Informatsiey, 7 bit characters.
mbcs_pos_t
size_t(* mbcs_pos_t)(const char *str, const char *ptr, bool &invalid)
for multi-byte character set encodings: gives the character position of the ptr
Definition: QoreEncoding.h:56
QoreEncoding::getCharLen
DLLEXPORT qore_offset_t getCharLen(const char *p, size_t valid_len) const
gives the number of total bytes for the next character at the given pointer
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QCS_ISO_8859_14
DLLEXPORT const QoreEncoding * QCS_ISO_8859_14
latin-8, Celtic character set
QoreEncoding::getUnicode
DLLEXPORT int getUnicode(const char *p, const char *end, unsigned &clen, ExceptionSink *xsink) const
returns the unicode code point for the given character; if there are any errors (invalid character...
QoreEncoding::getCharPos
DLLEXPORT size_t getCharPos(const char *p, const char *end, bool &invalid) const
gives the character position (number of characters) starting from the first pointer to the second ...
QCS_ISO_8859_6
DLLEXPORT const QoreEncoding * QCS_ISO_8859_6
Arabic character set.
qore_offset_t
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
QCS_ISO_8859_5
DLLEXPORT const QoreEncoding * QCS_ISO_8859_5
Cyrillic character set.
common.h
QoreThreadLock
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
QCS_UTF16LE
DLLEXPORT const QoreEncoding * QCS_UTF16LE
UTF-16LE (only UTF-8 and UTF-16* are multi-byte encodings)
QoreEncodingManager::add
static DLLEXPORT const QoreEncoding * add(const char *code, const char *desc=0, unsigned char maxwidth=1, mbcs_length_t l=0, mbcs_end_t e=0, mbcs_pos_t p=0, mbcs_charlen_t=0)
adds a new encoding to the list
QCS_ISO_8859_16
DLLEXPORT const QoreEncoding * QCS_ISO_8859_16
latin-10, Southeast European character set
QCS_ISO_8859_15
DLLEXPORT const QoreEncoding * QCS_ISO_8859_15
latin-9, Western European with euro symbol
QCS_KOI8_R
DLLEXPORT const QoreEncoding * QCS_KOI8_R
Russian: Kod Obmena Informatsiey, 8 bit.
QCS_ISO_8859_7
DLLEXPORT const QoreEncoding * QCS_ISO_8859_7
Greek character set.
QCS_ISO_8859_13
DLLEXPORT const QoreEncoding * QCS_ISO_8859_13
latin-7, Baltic rim character set