[前][次][番号順一覧][スレッド一覧]

ruby-changes:30585

From: nobu <ko1@a...>
Date: Fri, 23 Aug 2013 17:18:22 +0900 (JST)
Subject: [ruby-changes:30585] nobu:r42664 (trunk): win32ole.c: store directly

nobu	2013-08-23 17:17:59 +0900 (Fri, 23 Aug 2013)

  New Revision: 42664

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42664

  Log:
    win32ole.c: store directly
    
    * ext/win32ole/win32ole.c (ole_wc2vstr): store converted multibyte
      string to string value directly.

  Modified files:
    trunk/ext/win32ole/win32ole.c
Index: ext/win32ole/win32ole.c
===================================================================
--- ext/win32ole/win32ole.c	(revision 42663)
+++ ext/win32ole/win32ole.c	(revision 42664)
@@ -1064,7 +1064,7 @@ ole_cp2encoding(UINT cp) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1064
 }
 
 static char *
-ole_wc2mb(LPWSTR pw)
+ole_wc2mb_alloc(LPWSTR pw, char *(alloc)(UINT size, void *arg), void *arg)
 {
     LPSTR pm;
     UINT size = 0;
@@ -1076,7 +1076,7 @@ ole_wc2mb(LPWSTR pw) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1076
 	if (FAILED(hr)) {
             ole_raise(hr, eWIN32OLERuntimeError, "fail to convert Unicode to CP%d", cWIN32OLE_cp);
 	}
-	pm = ALLOC_N(char, size + 1);
+	pm = alloc(size, arg);
 	hr = pIMultiLanguage->lpVtbl->ConvertStringFromUnicode(pIMultiLanguage,
 		&dw, cWIN32OLE_cp, pw, NULL, pm, &size);
 	if (FAILED(hr)) {
@@ -1088,17 +1088,29 @@ ole_wc2mb(LPWSTR pw) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1088
     }
     size = WideCharToMultiByte(cWIN32OLE_cp, 0, pw, -1, NULL, 0, NULL, NULL);
     if (size) {
-        pm = ALLOC_N(char, size + 1);
+        pm = alloc(size, arg);
         WideCharToMultiByte(cWIN32OLE_cp, 0, pw, -1, pm, size, NULL, NULL);
         pm[size] = '\0';
     }
     else {
-        pm = ALLOC_N(char, 1);
+        pm = alloc(0, arg);
         *pm = '\0';
     }
     return pm;
 }
 
+static char *
+ole_alloc_str(UINT size, void *arg)
+{
+    return ALLOC_N(char, size + 1);
+}
+
+static char *
+ole_wc2mb(LPWSTR pw)
+{
+    return ole_wc2mb_alloc(pw, ole_alloc_str, NULL);
+}
+
 static VALUE
 ole_hresult2msg(HRESULT hr)
 {
@@ -1383,15 +1395,22 @@ ole_mb2wc(char *pm, int len) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L1395
     return pw;
 }
 
+static char *
+ole_alloc_vstr(UINT size, void *arg)
+{
+    VALUE str = rb_enc_str_new(NULL, size, cWIN32OLE_enc);
+    *(VALUE *)arg = str;
+    return RSTRING_PTR(str);
+}
+
 static VALUE
 ole_wc2vstr(LPWSTR pw, BOOL isfree)
 {
-    char *p = ole_wc2mb(pw);
-    VALUE vstr = rb_str_new_cstr(p);
-    rb_enc_associate(vstr, cWIN32OLE_enc);
+    VALUE vstr;
+    ole_wc2mb_alloc(pw, ole_alloc_vstr, &vstr);
+    rb_str_set_len(vstr, (long)strlen(RSTRING_PTR(vstr)));
     if(isfree)
         SysFreeString(pw);
-    free(p);
     return vstr;
 }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]