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/06/01 14:54:42 UTC
svn commit: r1345144 - in /incubator/etch/trunk/binding-cpp/runtime:
include/util/EtchUtil.h src/main/util/EtchUtil.cpp
src/test/util/EtchUtilTest.cpp
Author: veithm
Date: Fri Jun 1 12:54:41 2012
New Revision: 1345144
URL: http://svn.apache.org/viewvc?rev=1345144&view=rev
Log:
EtchUtil UTF-8 support
Change-Id: I244540a4543aaae31c03e9945ade09b4b9eae7a1
Modified:
incubator/etch/trunk/binding-cpp/runtime/include/util/EtchUtil.h
incubator/etch/trunk/binding-cpp/runtime/src/main/util/EtchUtil.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/util/EtchUtilTest.cpp
Modified: incubator/etch/trunk/binding-cpp/runtime/include/util/EtchUtil.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/util/EtchUtil.h?rev=1345144&r1=1345143&r2=1345144&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/util/EtchUtil.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/util/EtchUtil.h Fri Jun 1 12:54:41 2012
@@ -20,6 +20,7 @@
#define __ETCHUTIL_H__
#include "common/EtchError.h"
+#include "common/EtchConfig.h"
/*
* copy src to dst. Copies at most dst_size bytes. In case that
@@ -28,4 +29,14 @@
*/
status_t etch_strcpy_s(char *dst, size_t dstSize, const char *src);
+/**
+ * calculate the length of given string which is encoded in UTF8
+ * @param src string in UTF8
+ * @param length as out param
+ * @return ETCH_OK if length is successfully calculated
+ * ETCH_ERROR if length calculation has failed
+ * ETCH_EINVAL if src == NULL
+ */
+status_t etch_strlen_utf8(const char *src, capu::int32_t &length);
+
#endif
Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/util/EtchUtil.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/util/EtchUtil.cpp?rev=1345144&r1=1345143&r2=1345144&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/util/EtchUtil.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/util/EtchUtil.cpp Fri Jun 1 12:54:41 2012
@@ -19,31 +19,59 @@
#include "util/EtchUtil.h"
status_t etch_strcpy_s(char *dst, size_t dstSize, const char *src) {
- char *d, *end;
+ char *d, *end;
- if (dst == NULL || src == NULL) {
- return ETCH_EINVAL;
+ if (dst == NULL || src == NULL) {
+ return ETCH_EINVAL;
+ }
+
+ if (dstSize == 0) {
+ return ETCH_ERANGE;
+ }
+
+ d = dst;
+ end = dst + dstSize - 1;
+
+ for (; d < end; ++d, ++src) {
+ if (!(*d = *src)) {
+ return ETCH_OK;
}
+ }
- if (dstSize == 0) {
- return ETCH_ERANGE;
- }
-
- d = dst;
- end = dst + dstSize - 1;
-
- for (; d < end; ++d, ++src) {
- if (!(*d = *src)) {
- return ETCH_OK;
- }
- }
+ // if src buffer is not 0 now, then we have bytes left in src --> error
+ if (!(*d = *src)) {
+ return ETCH_OK;
+ } else {
+ // always null terminate
+ *d = '\0';
+ return ETCH_EINVAL;
+ }
+}
- // if src buffer is not 0 now, then we have bytes left in src --> error
- if(! (*d = *src)) {
- return ETCH_OK;
+status_t etch_strlen_utf8(const char *src, capu::int32_t &length) {
+ if (src == NULL)
+ return ETCH_EINVAL;
+ length = 0;
+ for (capu::int32_t i = 0; src[i] != 0;) {
+ if (!(src[i] & (0x80))) {
+ //1 byte character
+ i++;
+ length++;
+ } else if ((src[i] & (0xF0)) == 0xF0) {
+ //4 byte character
+ i = i + 4;
+ length++;
+ } else if ((src[i] & (0xE0)) == 0xE0) {
+ //3 byte character
+ i = i + 3;
+ length++;
+ } else if ((src[i] & (0xC0)) == 0xC0) {
+ //2 byte character
+ i = i + 2;
+ length++;
} else {
- // always null terminate
- *d = '\0';
- return ETCH_EINVAL;
+ return ETCH_ERROR;
}
+ }
+ return ETCH_OK;
}
Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/util/EtchUtilTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/util/EtchUtilTest.cpp?rev=1345144&r1=1345143&r2=1345144&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/util/EtchUtilTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/util/EtchUtilTest.cpp Fri Jun 1 12:54:41 2012
@@ -20,6 +20,7 @@
#include "util/EtchUtil.h"
// Tests etch_strcpy_s
+
TEST(EtchUtilTest, etch_strcpy_s) {
char* src = NULL;
char* dst = NULL;
@@ -43,3 +44,15 @@ TEST(EtchUtilTest, etch_strcpy_s) {
delete[] dst;
}
+
+TEST(EtchUtilTest, etch_utf8_strlen) {
+ const char utf8 [] = {(char)0xF0, (char)0xA4, (char)0xAD, (char)0xA2, (char)0xE2, (char)0x82, (char)0xAC, (char)0xC2, (char)0xA2, (char)0x24, (char)0x0};
+ capu::int32_t dstSize = 0;
+
+ status_t retval = etch_strlen_utf8(NULL, dstSize);
+ EXPECT_TRUE(retval == ETCH_EINVAL);
+
+ retval = etch_strlen_utf8(utf8, dstSize);
+ EXPECT_TRUE(retval == ETCH_OK);
+ EXPECT_TRUE(dstSize == 4);
+}