#At file:///home/malff/BZR-TREE/mysql-6.0-wl2110-review/
2683 Marc Alff 2008-08-25 [merge]
Partial merge
added:
sql/sql_fixstring.cc
sql/sql_fixstring.h
=== added file 'sql/sql_fixstring.cc'
--- a/sql/sql_fixstring.cc 1970-01-01 00:00:00 +0000
+++ b/sql/sql_fixstring.cc 2008-07-23 00:29:06 +0000
@@ -0,0 +1,158 @@
+/* Copyright (C) 2008 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#include "mysql_priv.h"
+
+Fixed_string::~Fixed_string()
+{}
+
+void Fixed_string::set(const char* str)
+{
+ set(str, strlen(str));
+}
+
+void Fixed_string::set(const char* str, size_t len)
+{
+ size_t numchars;
+ size_t to_copy;
+ CHARSET_INFO *cs= m_param->m_cs;
+ const char *end= str+len;
+
+ numchars= cs->cset->numchars(cs, str, end);
+
+ if (numchars <= m_param->m_max_char_length)
+ {
+ to_copy= len;
+ m_truncated= FALSE;
+ }
+ else
+ {
+ to_copy= cs->cset->charpos(cs, str, end, m_param->m_max_char_length);
+ m_truncated= TRUE;
+ }
+
+ reserve(to_copy + cs->mbminlen);
+ if (m_ptr)
+ {
+ m_byte_length= to_copy;
+ memcpy(m_ptr, str, to_copy);
+ add_nul(m_ptr + to_copy);
+ }
+}
+
+void Fixed_string::set(const String* str)
+{
+ set(str->ptr(), str->length(), (CHARSET_INFO*) str->charset());
+}
+
+void Fixed_string::set(const char* str, size_t len, const CHARSET_INFO *str_cs)
+{
+ uint32 dummy_offset;
+ size_t numchars;
+ size_t to_copy;
+ CHARSET_INFO *cs= m_param->m_cs;
+ const char *end= str + len;
+
+ numchars= cs->cset->numchars(cs, str, end);
+
+ if (numchars <= m_param->m_max_char_length)
+ {
+ to_copy= len;
+ m_truncated= FALSE;
+ }
+ else
+ {
+ numchars= m_param->m_max_char_length;
+ to_copy= cs->cset->charpos(cs, str, end, numchars);
+ m_truncated= TRUE;
+ }
+
+ if (String::needs_conversion(to_copy, (CHARSET_INFO*) str_cs,
+ (CHARSET_INFO*) cs, & dummy_offset))
+ {
+ size_t dest_len= numchars * cs->mbmaxlen;
+ reserve(dest_len + cs->mbminlen);
+ if (m_ptr)
+ {
+ const char* well_formed_error_pos;
+ const char* cannot_convert_error_pos;
+ const char* from_end_pos;
+
+ m_byte_length= well_formed_copy_nchars((CHARSET_INFO *) cs,
+ m_ptr, dest_len,
+ (CHARSET_INFO*) str_cs, str, len,
+ numchars,
+ & well_formed_error_pos,
+ & cannot_convert_error_pos,
+ & from_end_pos);
+ add_nul(m_ptr + m_byte_length);
+ }
+ }
+ else
+ {
+ reserve(to_copy + cs->mbminlen);
+ if (m_ptr)
+ {
+ m_byte_length= to_copy;
+ memcpy(m_ptr, str, to_copy);
+ add_nul(m_ptr + to_copy);
+ }
+ }
+}
+
+void Fixed_string::add_nul(char *ptr)
+{
+ /*
+ Assume that the NUL character is always encoded using 0 bytes,
+ and is always using an encoding of the minimum length.
+ Verified for:
+ - UTF8 : NUL = 1 zero byte
+ - latin* : NUL = 1 zero byte
+ - UTF16 : NUL = 2 zero byte
+ - UTF32 : NUL = 4 zero byte
+ */
+ switch (m_param->m_cs->mbminlen)
+ {
+ case 4: *ptr++ = '\0'; /* fall through */
+ case 3: *ptr++ = '\0'; /* fall through */
+ case 2: *ptr++ = '\0'; /* fall through */
+ case 1: *ptr++ = '\0'; break;
+ default: DBUG_ASSERT(FALSE); break;
+ }
+}
+
+void Fixed_string::copy(const Fixed_string *str)
+{
+ DBUG_ASSERT(m_param->m_cs->number == str->m_param->m_cs->number);
+
+ set(str->m_ptr, str->m_byte_length);
+}
+
+void Fixed_string::reserve(size_t len)
+{
+ if ((m_ptr != NULL) && (m_allocated_length >= len))
+ return;
+
+ m_ptr= (char*) alloc_root(m_mem_root, len);
+ m_allocated_length= (m_ptr ? len : 0);
+}
+
+const Fixed_string_param UTF8String64::params=
+{64, & my_charset_utf8_bin };
+
+const Fixed_string_param UTF8String128::params=
+{128, & my_charset_utf8_bin };
+
+
=== added file 'sql/sql_fixstring.h'
--- a/sql/sql_fixstring.h 1970-01-01 00:00:00 +0000
+++ b/sql/sql_fixstring.h 2008-07-23 00:29:06 +0000
@@ -0,0 +1,224 @@
+/* Copyright (C) 2008 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef SQL_FIXSTRING_H
+#define SQL_FIXSTRING_H
+
+/**
+ This structure represents the parametrized metadata used
+ in class Fixed_string.
+ The metadata consist of 'N' and 'C' in
+ VARCHAR(N) CHARACTER SET C
+ Multiple instances of Fixed_string typically share the same
+ Fixed_string_param instance.
+*/
+struct Fixed_string_param
+{
+ /**
+ Maximum length of a VARCHAR string (N), in VARCHAR(N)
+ */
+ size_t m_max_char_length;
+ /**
+ Character set of a VARCHAR string (C), in VARCHAR(N) CHARACTER SET C.
+ */
+ CHARSET_INFO *m_cs;
+};
+
+/**
+ This class represents a VARCHAR string or a given fixed maximum size,
+ and of a given fixed character set.
+ The size and character set are immutable.
+ Memory used to represent the string is allocated from a provided memory root.
+*/
+class Fixed_string
+{
+public:
+ /**
+ Constructor.
+ @param param Immutable size and character set parameters
+ @param mem_root Memory root to use to represent the string value
+ */
+ Fixed_string(const Fixed_string_param *param, MEM_ROOT *mem_root)
+ : m_param(param),
+ m_mem_root(mem_root),
+ m_byte_length(0),
+ m_allocated_length(0),
+ m_truncated(FALSE),
+ m_ptr(NULL)
+ {}
+
+ /** Destructor. */
+ ~Fixed_string();
+
+ /**
+ Set the string value.
+ @param str the value to set, expressed in the character set of
+ Fixed_string_param
+ */
+ void set(const char* str);
+
+ /**
+ Set the string value.
+ @param str the value to set, expressed in the character set of
+ Fixed_string_param
+ @param len length, in bytes, of str
+ */
+ void set(const char* str, size_t len);
+
+ /**
+ Set the string value, with character set conversion if necessary.
+ @param str the value to set, expressed in any character set
+ */
+ void set(const String* str);
+
+ /**
+ Set the string value, with character set conversion if necessary.
+ @param str the value to set, expressed in character set str_cs
+ @param len length, in bytes, of str
+ @param str_cs character set of str
+ */
+ void set(const char* str, size_t len, const CHARSET_INFO *str_cs);
+
+ /**
+ Predicate, indicates if the string is truncated to the maximum size.
+ @return true if the string is truncated
+ */
+ bool is_truncated() const
+ { return m_truncated; }
+
+ /**
+ Access to the C pointer representation of the string.
+ @return a NUL terminated C string, in the character set of
+ Fixed_string_param
+ */
+ const char* ptr() const
+ { return m_ptr; }
+
+ /**
+ Length, in bytes, of the C string representation,
+ excluding the terminating NUL character.
+ @return The string length in bytes
+ */
+ size_t length() const
+ { return m_byte_length; }
+
+ /**
+ Character set of the string.
+ @return the string character set
+ */
+ const CHARSET_INFO *charset() const
+ { return m_param->m_cs; }
+
+ /**
+ Set the string value from another Fixed_string.
+ Note that the character set of this object and of str must be equal.
+ @param str the string to copy
+ */
+ void copy(const Fixed_string *str);
+
+private:
+ /**
+ Allocate memory for the string representation.
+ @param len size in bytes to allocate
+ */
+ void reserve(size_t len);
+
+ /**
+ Add a NUL character at the end of the string.
+ @param ptr location of the NUL character
+ */
+ void add_nul(char *ptr);
+
+ /**
+ Immutable string parameters.
+ The string parameters are <N> and <C>
+ in a parametrized class 'VARCHAR(<N>) CHARACTER SET <C>'
+ */
+ const Fixed_string_param *m_param;
+
+ /**
+ Memory root to use to allocate the string value.
+ */
+ MEM_ROOT *m_mem_root;
+
+ /**
+ Length of the C string representation, in bytes,
+ excluding the terminating NUL character.
+ */
+ size_t m_byte_length;
+
+ /**
+ Size, in bytes, of the memory allocated.
+ */
+ size_t m_allocated_length;
+
+ /**
+ True if the string was truncated.
+ Note that no warnings or errors are generated,
+ the string is truncated silently.
+ */
+ bool m_truncated;
+
+ /**
+ C representation of the string, NUL terminated.
+ */
+ char * m_ptr;
+};
+
+/**
+ This class represents a 'VARCHAR(64) CHARACTER SET UTF8' string value.
+*/
+class UTF8String64 : public Fixed_string
+{
+public:
+ /**
+ Constructor.
+ @param root The memory root to use to represent the string value.
+ */
+ UTF8String64(MEM_ROOT *root)
+ : Fixed_string(& params, root)
+ {}
+
+ ~UTF8String64()
+ {}
+
+private:
+ static const Fixed_string_param params;
+};
+
+/**
+ This class represents a 'VARCHAR(128) CHARACTER SET UTF8' string value.
+*/
+class UTF8String128 : public Fixed_string
+{
+public:
+ /**
+ Constructor.
+ @param root The memory root to use to represent the string value.
+ */
+ UTF8String128(MEM_ROOT *root)
+ : Fixed_string(& params, root)
+ {}
+
+ ~UTF8String128()
+ {}
+
+private:
+ static const Fixed_string_param params;
+};
+
+#endif
+
+
| Thread |
|---|
| • bzr commit into mysql-6.0-wl2110-review branch (marc.alff:2683) | Marc Alff | 26 Aug |