You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/01/12 11:03:27 UTC

svn commit: r1230463 - in /incubator/etch/trunk/binding-cpp/runtime: include/common/EtchString.h src/main/common/EtchString.cpp src/test/common/EtchStringTest.cpp

Author: veithm
Date: Thu Jan 12 10:03:27 2012
New Revision: 1230463

URL: http://svn.apache.org/viewvc?rev=1230463&view=rev
Log:
ETCH-112 EtchString Implementation

contains implementation of these methods

substring
copy constructor
Assignment operator overloading
hash function
equals method

Change-Id: Ib197140957a2a5f1e771d443dfa8137dedfdea49

Modified:
    incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchStringTest.cpp

Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h?rev=1230463&r1=1230462&r2=1230463&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchString.h Thu Jan 12 10:03:27 2012
@@ -1,80 +1,143 @@
-/* $Id$ 
- * 
- * Licensed to the Apache Software Foundation (ASF) under one or more 
- * contributor license agreements. See the NOTICE file distributed with  
- * this work for additional information regarding copyright ownership. 
- * The ASF licenses this file to you under the Apache License, Version  
- * 2.0 (the "License"); you may not use this file except in compliance  
- * with the License. You may obtain a copy of the License at 
- * 
- * http://www.apache.org/licenses/LICENSE-2.0 
- * 
- * Unless required by applicable law or agreed to in writing, software 
- * distributed under the License is distributed on an "AS IS" BASIS, 
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and 
- * limitations under the License. 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #ifndef __ETCHSTRING_H__
 #define __ETCHSTRING_H__
 
 #include "common/EtchObject.h"
+#include "util/EtchUtil.h"
 
 /**
  * String type.
  */
 class EtchString :
-    public EtchObject
-{
+public EtchObject {
+
 public:
 
-    /**
-     * TypeId for EtchString.
-     */
-    static const capu::int32_t TYPE_ID = EOTID_STRING;
-
-    /**
-     * Constructs the String.
-     */
-    EtchString();
-
-    /**
-     * Constructs the String.
-     * @param string as c string
-     */
-    EtchString(const char* string);
-    
-    /**
-     * Destructure.
-     */
-    virtual ~EtchString();
-
-    /**
-     * Set a new string.
-     * @param string as c string
-     */
-    void set(const char* string);
-    
-    /**
-     * Set a new string.
-     * @param string as c string
-     * @param len as unsigned integer to represent size of string
-     */
-    void set(const char* string, capu::uint32_t len);
-
-    /**
-     * Returns the amount of characters in the string.
-     */
-    capu::int32_t lenght();
-
-    /**
-     * Returns c styled string.
-     */
-    const char* c_str();
+  /**
+   * TypeId for EtchString.
+   */
+  static const capu::int32_t TYPE_ID = EOTID_STRING;
+
+  /**
+   * Constructs the String.
+   */
+  EtchString();
+
+  /**
+   * Copy constructor
+   * @param copy
+   */
+  EtchString(const EtchString & copy);
+
+  /**
+   * Constructs the String.
+   * @param string as c string
+   */
+  EtchString(const char* string);
+
+  /**
+   * Destructure.
+   */
+  virtual ~EtchString();
+
+  /**
+   * Set a new string.
+   * @param string as c string
+   */
+  void set(const char* string);
+
+  /**
+   * Set a new string.
+   * @param string as c string
+   * @param len as unsigned integer to represent size of string
+   */
+  void set(const char* string, capu::uint32_t len);
+
+  /**
+   * Returns the amount of characters in the string.
+   * @return amount of characters
+   */
+  capu::int32_t length();
+
+  /**
+   * the substring is generated.
+   * note: User must explicitly deallocate the returned object
+   * @param start starting index of substring
+   * @param length length of given string
+   * @param dest where the substring will be stored
+   * @return ETCH_OK if the substring has been successfully stored into the dest
+   *         ETCH_EINVAL if there are errors in the indexing or buffer is NULL
+   */
+  status_t substring(capu::uint32_t start, capu::uint32_t length, EtchString *dest);
+
+  /**
+   * Returns the index of the first occurrence of a character within given string
+   * @param c
+   * @return index index of first occurrence of character
+   *         -1 if the character is not found
+   */
+  capu::int32_t leftFind(const char c);
+
+  /**
+   * Returns the index of last occurrence of a character within given string
+   * @param c
+   * @return index index of last occurrence of character
+   *         -1 if the character is not found
+   */
+  capu::int32_t rightFind(const char c);
+
+  /**
+   * Check two string is equal or not
+   * @param other
+   * @return true if this equals b, false otherwise. takes into account nulls.
+   */
+  capu::bool_t equals(const EtchObject * other);
+
+  /**
+   * Returns c styled string.
+   */
+  const char* c_str();
+
+  /**
+   * Assignment operator overloading
+   */
+  EtchString& operator=(const EtchString &str);
+
+  /**
+   * Assignment operator overloading
+   */
+  EtchString& operator=(const EtchString *str);
+
+  /**
+   * Assignment operator overloading
+   */
+  EtchString& operator=(const char *str);
+
+  /**
+   * Returns hash code
+   */
+  capu::uint64_t getHashCode();
 
 private:
-    char* m_data;
+
+  char* mData;
 };
 
 #endif

Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp?rev=1230463&r1=1230462&r2=1230463&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/common/EtchString.cpp Thu Jan 12 10:03:27 2012
@@ -1,85 +1,151 @@
-/* $Id$ 
- * 
- * Licensed to the Apache Software Foundation (ASF) under one or more 
- * contributor license agreements. See the NOTICE file distributed with  
- * this work for additional information regarding copyright ownership. 
- * The ASF licenses this file to you under the Apache License, Version  
- * 2.0 (the "License"); you may not use this file except in compliance  
- * with the License. You may obtain a copy of the License at 
- * 
- * http://www.apache.org/licenses/LICENSE-2.0 
- * 
- * Unless required by applicable law or agreed to in writing, software 
- * distributed under the License is distributed on an "AS IS" BASIS, 
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and 
- * limitations under the License. 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include "common/EtchString.h"
-#include "util/EtchUtil.h"
 
 EtchString::EtchString()
-    : EtchObject(EtchString::TYPE_ID)
-    , m_data(NULL)
-{
-    //ETCH_LOG("EtchString: ", EtchLogLevel::Error, "dies ist ein Test");
+: EtchObject(EtchString::TYPE_ID)
+, mData(NULL) {
+  //ETCH_LOG("EtchString: ", EtchLogLevel::Error, "dies ist ein Test");
 }
 
 EtchString::EtchString(const char* string)
-    : EtchObject(EtchString::TYPE_ID)
-    , m_data(NULL)
-{
-    if(string != NULL) {
-        size_t len = strlen(string);
-        m_data = new char[len + 1];
-        etch_strcpy_s(m_data, len + 1, string);
-    }
-}
-
-EtchString::~EtchString()
-{
-    if(m_data != NULL) {
-        delete[] m_data;
-        m_data = NULL;
-    }
-}
-
-void EtchString::set(const char* string)
-{
-    if(m_data != NULL) {
-        delete[] m_data;
-        m_data = NULL;
-    }
-    if(string != NULL) {
-        size_t len = strlen(string);
-        m_data = new char[len + 1];
-        etch_strcpy_s(m_data, len + 1, string);
-    }
-}
-
-void EtchString::set(const char* string,capu::uint32_t len)
-{
-    if(m_data != NULL) {
-        delete[] m_data;
-        m_data = NULL;
-    }
-    if(string != NULL) {
-        if(strlen(string)<len)
-            return;
-        size_t length = len;
-        m_data = new char[length + 1];
-        etch_strcpy_s(m_data, length + 1, string);
-    }
-    
-}
-
-capu::int32_t EtchString::lenght()
-{
-    return 0;
-}
-
-const char* EtchString::c_str()
-{
-    return m_data;
+: EtchObject(EtchString::TYPE_ID)
+, mData(NULL) {
+  if (string != NULL) {
+    capu::int32_t len = strlen(string);
+    mData = new char[len + 1];
+    etch_strcpy_s(mData, len + 1, string);
+  }
+}
+
+EtchString::EtchString(const EtchString &copy)
+: EtchObject(EtchString::TYPE_ID) {
+  if (copy.mData == NULL)
+    return;
+  capu::int32_t len = strlen(copy.mData);
+  mData = new char [len + 1];
+  etch_strcpy_s(mData, len + 1, copy.mData);
+}
+
+EtchString& EtchString::operator=(const char *str) {
+  if (this->mData != str) {
+    set(str);
+  }
+  return *this;
+}
+
+void EtchString::set(const char* string, capu::uint32_t len) {
+  if (mData != NULL) {
+    delete[] mData;
+    mData = NULL;
+  }
+  if (string != NULL) {
+    if (strlen(string) < len)
+      return;
+    capu::int32_t length = len;
+    mData = new char[length + 1];
+    etch_strcpy_s(mData, length + 1, string);
+  }
+
+}
+
+EtchString& EtchString::operator=(const EtchString &str) {
+  if (this != &str) {
+    set(str.mData);
+  }
+  return *this;
+}
+
+EtchString::~EtchString() {
+  if (mData != NULL) {
+    delete[] mData;
+    mData = NULL;
+  }
+}
+
+void EtchString::set(const char* string) {
+  if (mData != NULL) {
+    delete[] mData;
+    mData = NULL;
+  }
+  if (string != NULL) {
+    capu::int32_t len = strlen(string);
+    mData = new char[len + 1];
+    etch_strcpy_s(mData, len + 1, string);
+  }
+}
+
+capu::int32_t EtchString::length() {
+  return strlen(mData);
+}
+
+const char* EtchString::c_str() {
+  return mData;
+}
+
+capu::bool_t EtchString::equals(const EtchObject* other) {
+  if (other == NULL)
+    return false;
+  else if (other->getObjectTypeId() != EtchString::TYPE_ID)
+    return false;
+  if (strcmp(((EtchString*) other)->mData, this->mData) == 0)
+    return true;
+  return false;
+}
+
+capu::int32_t EtchString::rightFind(const char c) {
+  const char * str = this->c_str();
+  capu::int32_t index = -1;
+  char* ch = strchr((char *) str, c);
+  while (ch != NULL) {
+    index = ch - str;
+    ch = strchr(ch + 1, c);
+  }
+  return index;
+}
+
+capu::int32_t EtchString::leftFind(const char c) {
+  const char * str = this->c_str();
+  capu::int32_t index = -1;
+  char* ch = strchr((char*) str, c);
+  //NOT FOUND
+  if (ch == NULL)
+    return -1;
+  index = ch - str;
+  return index;
+}
+
+status_t EtchString::substring(capu::uint32_t start, capu::uint32_t length, EtchString *dest) {
+  capu::uint32_t len = strlen(this->mData);
+  if (start >= len || len < (start + length) || dest == NULL) {
+    return ETCH_EINVAL;
+  } else {
+    dest->set(&this->mData[start], length);
+    return ETCH_OK;
+  }
+}
+
+capu::uint64_t EtchString::getHashCode() {
+  capu::uint64_t result = 0;
+  capu::uint32_t len = strlen(mData);
+  for (capu::uint32_t i = 0; i < len; i++) {
+    result = (result + static_cast<capu::uint64_t> (mData[i]) * 13);
+  }
+  return result;
 }

Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchStringTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchStringTest.cpp?rev=1230463&r1=1230462&r2=1230463&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchStringTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchStringTest.cpp Thu Jan 12 10:03:27 2012
@@ -1,55 +1,138 @@
-/* $Id$ 
- * 
- * Licensed to the Apache Software Foundation (ASF) under one or more 
- * contributor license agreements. See the NOTICE file distributed with  
- * this work for additional information regarding copyright ownership. 
- * The ASF licenses this file to you under the Apache License, Version  
- * 2.0 (the "License"); you may not use this file except in compliance  
- * with the License. You may obtain a copy of the License at 
- * 
- * http://www.apache.org/licenses/LICENSE-2.0 
- * 
- * Unless required by applicable law or agreed to in writing, software 
- * distributed under the License is distributed on an "AS IS" BASIS, 
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and 
- * limitations under the License. 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include <gtest/gtest.h>
 #include "common/EtchString.h"
 
 // Tests positive input.
+
 TEST(EtchStringTest, Constructor_Default) {
-    EtchString* s1 = new EtchString();
-    EXPECT_TRUE(s1->getObjectTypeId() == EtchString::TYPE_ID);
-    EXPECT_TRUE(s1->c_str() == NULL);
-    delete s1;
+  EtchString* s1 = new EtchString();
+  EXPECT_TRUE(s1->getObjectTypeId() == EtchString::TYPE_ID);
+  EXPECT_TRUE(s1->c_str() == NULL);
+  delete s1;
 }
 
 TEST(EtchStringTest, Constructor_Char) {
-    EtchString* s1 = new EtchString("test");
-    EXPECT_TRUE(s1->getObjectTypeId() == EtchString::TYPE_ID);
-    EXPECT_TRUE(s1->c_str() != NULL);
-    EXPECT_TRUE(strcmp(s1->c_str(), "test") == 0);
-    delete s1;
+  EtchString* s1 = new EtchString("test");
+  EXPECT_TRUE(s1->getObjectTypeId() == EtchString::TYPE_ID);
+  EXPECT_TRUE(s1->c_str() != NULL);
+  EXPECT_TRUE(strcmp(s1->c_str(), "test") == 0);
+  delete s1;
 }
 
 TEST(EtchStringTest, set) {
-    EtchString* s1 = new EtchString("test");
-    s1->set("hello");
-    EXPECT_TRUE(s1->c_str() != NULL);
-    EXPECT_TRUE(strcmp(s1->c_str(), "hello") == 0);
-    delete s1;
+  EtchString* s1 = new EtchString("test");
+  s1->set("hello");
+  EXPECT_TRUE(s1->c_str() != NULL);
+  EXPECT_TRUE(strcmp(s1->c_str(), "hello") == 0);
+  delete s1;
 }
+
 TEST(EtchStringTest, setLen) {
-    EtchString* s1 = new EtchString("test");
-    s1->set("hello",3);
-    EXPECT_TRUE(s1->c_str() != NULL);
-    EXPECT_TRUE(strcmp(s1->c_str(), "hel") == 0);
-    delete s1;
+  EtchString* s1 = new EtchString("test");
+  s1->set("hello", 3);
+  EXPECT_TRUE(s1->c_str() != NULL);
+  EXPECT_TRUE(strcmp(s1->c_str(), "hel") == 0);
+  delete s1;
+}
+
+TEST(EtchStringTest, assign) {
+  EtchString *s = new EtchString("hello");
+  *s = "hallo2";
+  EXPECT_TRUE(strcmp(s->c_str(), "hallo2") == 0);
+  EtchString _tmp("hi");
+  *s = _tmp;
+  EXPECT_TRUE(strcmp(s->c_str(), "hi") == 0);
+  delete s;
 }
 
 TEST(EtchStringTest, c_str) {
-    EXPECT_TRUE(true);
+  EtchString *s = new EtchString(NULL);
+  EXPECT_TRUE(s->c_str() == NULL);
+
+  *s = "hello";
+  EXPECT_TRUE(strcmp(s->c_str(), "hello") == 0);
+
+  EtchString as("hallo2");
+  EXPECT_TRUE(strcmp(as.c_str(), "hallo2") == 0);
+
+  delete s;
+}
+
+TEST(EtchStringTest, equalsTest) {
+  EtchString str1("test");
+  EtchString str2("test");
+  EtchString str3("test2");
+
+  EXPECT_TRUE(str1.equals(&str2));
+  EXPECT_FALSE(str1.equals(&str3));
+}
+
+TEST(EtchStringTest, length) {
+  EtchString str1("test");
+  EtchString str2("");
+
+  EXPECT_EQ(4, str1.length());
+  EXPECT_EQ(0, str2.length());
 }
+
+TEST(EtchStringTest, leftFindTest) {
+  EtchString str1("test1:test2:test3");
+  capu::int32_t index = str1.leftFind(':');
+  EXPECT_TRUE(index == 5);
+  index = str1.leftFind('<');
+  EXPECT_TRUE(index == -1);
+}
+
+TEST(EtchStringTest, rightFindTest) {
+  EtchString str1("test1:test2:test3");
+  capu::int32_t index = str1.rightFind(':');
+  EXPECT_TRUE(index == 11);
+  index = str1.rightFind('<');
+  EXPECT_TRUE(index == -1);
+}
+
+TEST(EtchStringTest, substringTest) {
+  EtchString str1("test1:test2:test3");
+
+  EtchString tmp;
+  status_t result;
+  result = str1.substring(0, 5, &tmp);
+  EXPECT_TRUE(result == ETCH_OK);
+  EXPECT_TRUE(strcmp("test1", tmp.c_str()) == 0);
+
+  result = str1.substring(0, 5, NULL);
+  EXPECT_TRUE(result == ETCH_EINVAL);
+
+  result = str1.substring(0, 90, &tmp);
+  EXPECT_TRUE(result == ETCH_EINVAL);
+
+  result = str1.substring(-5, 7, &tmp);
+  EXPECT_TRUE(result == ETCH_EINVAL);
+
+  result = str1.substring(5, -7, &tmp);
+  EXPECT_TRUE(result == ETCH_EINVAL);
+
+  result = str1.substring(-5, -7, &tmp);
+  EXPECT_TRUE(result == ETCH_EINVAL);
+
+  result = str1.substring(str1.length() - 1, 1, &tmp);
+  EXPECT_TRUE(result == ETCH_OK);
+  EXPECT_TRUE(strcmp("3", tmp.c_str()) == 0);
+}
\ No newline at end of file