X-Account-Key: account4
X-UIDL: 1153746480.180775
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
Return-Path: <commits-return-50239-marc.alff=mysql.com@lists.mysql.com>
Received: from mailget.mysql.com ([unix socket])
	 by mailget (Cyrus v2.3.1-Invoca-RPM-2.3.1-2.8.fc5) with LMTPA;
	 Wed, 23 Jul 2008 02:29:24 +0200
X-Sieve: CMU Sieve 2.3
Received: from mail.mysql.com (mail.mysql.com [10.100.1.21])
	by mailget.mysql.com (8.13.8/8.13.8) with ESMTP id m6N0TO1L010781
	for <malff@mailstore1.mysql.com>; Wed, 23 Jul 2008 02:29:24 +0200
Received: from mailgate-a.mysql.com (mailgate-a.mysql.com [10.128.2.13])
	by mail.mysql.com (8.13.3/8.13.3) with ESMTP id m6N0TLOR010318
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK)
	for <marc.alff@mysql.com>; Wed, 23 Jul 2008 02:29:22 +0200
Received: from lists.mysql.com (lists.mysql.com [10.100.1.37])
	by mailgate-a.mysql.com (8.14.1/8.14.1) with SMTP id m6N0MB6v018349
	for <marc.alff@mysql.com>; Wed, 23 Jul 2008 02:22:11 +0200
Received: (qmail 24586 invoked by uid 510); 23 Jul 2008 00:29:18 -0000
Mailing-List: contact commits-help@lists.mysql.com; run by ezmlm
List-ID: <commits.mysql.com>
Precedence: bulk
List-Help: <mailto:commits-help@lists.mysql.com>
List-Unsubscribe: <mailto:commits-unsubscribe-marc.alff=mysql.com@lists.mysql.com>
List-Post: <mailto:commits@lists.mysql.com>
List-Archive: http://lists.mysql.com/commits/50239
Delivered-To: mailing list commits@lists.mysql.com
Received: (qmail 24561 invoked by uid 509); 23 Jul 2008 00:29:18 -0000
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
From: Marc Alff <marc.alff@sun.com>
Subject: bzr commit into mysql-6.0-wl2110-review branch (marc.alff:2675) WL#2110
To: commits@lists.mysql.com
User-Agent: Bazaar (1.4)
X-CSetKey: marc.alff@sun.com-20080723002906-5jwc8m9tgkpomcqd
X-Worklog: 2110
X-bzr-action: commit
X-bzr-revid: marc.alff@sun.com-20080723002906-5jwc8m9tgkpomcqd
X-bzr-revno: 2675
Message-Id: <20080723002911.E53CB1DC614@lambda.WEBLAB>
Date: Tue, 22 Jul 2008 18:29:11 -0600 (MDT)
X-Cxn-Txn: 56108152,34985479

#At file:///home/malff/BZR-TREE/mysql-6.0-wl2110-review-part8/

 2675 Marc Alff	2008-07-22
      WL#2110 (Stored Procedures: Implement SIGNAL)
      
      Formal review part 8/10: Utilities
      
      This patch implements VARCHAR(N) CHARACTER SET UTF8 helper strings.
      
        sql/sql_fixstring.cc
        sql/sql_fixstring.h
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 &lt;N&gt; and &lt;C&gt;
+    in a parametrized class 'VARCHAR(&lt;N&gt;) CHARACTER SET &lt;C&gt;'
+  */
+  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
+
+


-- 
MySQL Code Commits Mailing List
For list archives: http://lists.mysql.com/commits
To unsubscribe:    http://lists.mysql.com/commits?unsub=marc.alff@mysql.com


