OSDN Git Service

* speed up.(little)
authorkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Wed, 30 Apr 2008 16:46:44 +0000 (16:46 +0000)
committerkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Wed, 30 Apr 2008 16:46:44 +0000 (16:46 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/branches/RELEASE_0_12_0@2586 1a406e8e-add9-4483-a2c8-d8cac5b7c224

src/chxj_url_encode.c
test/chxj_url_encode/test_chxj_url_encode.c

index d657bf1..b19b9a9 100644 (file)
@@ -42,35 +42,32 @@ chxj_url_encode(apr_pool_t *pool, const char *src)
   char *dst;
   char *sp = (char *)src;
   unsigned char tmp;
+  int len;
+  int pos;
 
-  dst = apr_palloc(pool, 1);
-  dst[0] = 0;
-
-  if (!src) return dst;
+  if (! src) return apr_pstrdup(pool, "\0");
 
+  len = strlen(src) * 3 + 1;
+  dst = apr_palloc(pool, len);
+  memset(dst, 0, len);
+  pos = 0;
 
   while(*sp) {
-
-    if (IS_ALPHA_UPPER(*sp) ||  IS_ALPHA_LOWER(*sp) ||  IS_DIGIT(*sp)) {
-      dst = apr_psprintf(pool, "%s%c", dst, *sp);
-      sp++;
+    if (IS_ALPHA_UPPER(*sp) || IS_ALPHA_LOWER(*sp) || IS_DIGIT(*sp)) {
+      dst[pos++] = *sp++;
       continue;
     }
-
     if (*sp == ' ') {
-      dst = apr_pstrcat(pool, dst, "+", NULL);
+      dst[pos++] = '+';
       sp++;
       continue;
     }
 
-    tmp = (*sp >> 4) & 0x0f;
-    dst = apr_psprintf(pool, "%s%%%c", dst, TO_HEXSTRING(tmp));
-    tmp = *sp & 0x0f;
-    dst = apr_psprintf(pool, "%s%c", dst,   TO_HEXSTRING(tmp));
-
+    dst[pos++] = '%';
+    dst[pos++] = TO_HEXSTRING((*sp >> 4) & 0x0f);
+    dst[pos++] = TO_HEXSTRING((*sp & 0x0f));
     sp++;
   }
-  
   return dst;
 }
 
index a51c27c..87b2ade 100644 (file)
@@ -40,6 +40,7 @@ void test_chxj_url_encode_020();
 void test_chxj_url_encode_021();
 void test_chxj_url_encode_022();
 void test_chxj_url_encode_023();
+void test_chxj_url_encode_024();
 /* pend */
 
 int
@@ -74,6 +75,7 @@ main()
   CU_add_test(str_util_suite, "chxj_url_encode 021",                                  test_chxj_url_encode_021);
   CU_add_test(str_util_suite, "chxj_url_encode 022",                                  test_chxj_url_encode_022);
   CU_add_test(str_util_suite, "chxj_url_encode 023",                                  test_chxj_url_encode_023);
+  CU_add_test(str_util_suite, "chxj_url_encode 024",                                  test_chxj_url_encode_024);
   /* aend */
 
   CU_basic_run_tests();
@@ -523,6 +525,24 @@ void test_chxj_url_encode_023()
 #undef TEST_STRING
 #undef RESULT_STRING
 }
+void test_chxj_url_encode_024()
+{
+#define  TEST_STRING   "改正租税特別措置法の衆院での再可決をめぐり、民主党議員が30日、国会内の衆院議長応接室を封鎖して、河野洋平衆院議長の本会議場入場"
+#define  RESULT_STRING ""
+  char *ret;
+  APR_INIT;
+
+  ret = chxj_url_encode(p, TEST_STRING);
+  fprintf(stderr, "actual:[%s]\n", ret);
+  fprintf(stderr, "expect:[%s]\n", RESULT_STRING);
+#if 0
+  CU_ASSERT(strcmp(ret, RESULT_STRING) == 0);
+#endif
+
+  APR_TERM;
+#undef TEST_STRING
+#undef RESULT_STRING
+}
 /*
  * vim:ts=2 et
  */