add strsubst function and manage regexp on wide char strings 49/1949/6
antoine ELIAS [Thu, 16 Sep 2010 14:07:13 +0000 (16:07 +0200)]
Change-Id: I8c5083f8c475f67d6ffb5b888c4442029d29fb50

16 files changed:
scilab/Scilab.sln
scilab/modules/string/Makefile.am
scilab/modules/string/Makefile.in
scilab/modules/string/includes/gw_string.h
scilab/modules/string/includes/string_gw.hxx
scilab/modules/string/includes/strsubst.h
scilab/modules/string/sci_gateway/c/sci_strsubst.c [deleted file]
scilab/modules/string/sci_gateway/cpp/sci_regexp.cpp
scilab/modules/string/sci_gateway/cpp/sci_strindex.cpp
scilab/modules/string/sci_gateway/cpp/sci_strsubst.cpp [new file with mode: 0644]
scilab/modules/string/sci_gateway/cpp/string_gw.cpp
scilab/modules/string/sci_gateway/cpp/string_gw.vcproj
scilab/modules/string/src/c/pcre_private.c
scilab/modules/string/src/c/pcre_private.h
scilab/modules/string/src/c/string.vcproj
scilab/modules/string/src/c/strsubst.c

index e09cb22..8c4726f 100644 (file)
@@ -1484,6 +1484,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "string_gw", "modules\string
        ProjectSection(ProjectDependencies) = postProject
                {ECFFEB0C-1EDA-45EE-9A10-B18143852E17} = {ECFFEB0C-1EDA-45EE-9A10-B18143852E17}
                {2C60033B-0DBD-4CA4-80D3-176C9BE9CE2F} = {2C60033B-0DBD-4CA4-80D3-176C9BE9CE2F}
+               {64BC155E-292F-484F-9404-76EE6AE67A43} = {64BC155E-292F-484F-9404-76EE6AE67A43}
                {8028F371-6A94-4A26-8804-6E7F05F1D1AA} = {8028F371-6A94-4A26-8804-6E7F05F1D1AA}
                {8D45767A-9B03-4905-97F6-D2F3F79141EA} = {8D45767A-9B03-4905-97F6-D2F3F79141EA}
                {6A84AD8C-BA18-4A3F-9C72-EC3DF0939E9E} = {6A84AD8C-BA18-4A3F-9C72-EC3DF0939E9E}
index c05af4c..dac463e 100644 (file)
@@ -40,7 +40,6 @@ sci_gateway/c/sci_str2code.c \
 sci_gateway/c/sci_strcat.c \
 sci_gateway/c/sci_string.c \
 sci_gateway/c/sci_strsplit.c \
-sci_gateway/c/sci_strsubst.c \
 sci_gateway/c/sci_tokens.c \
 sci_gateway/c/sci_strcmp.c \
 sci_gateway/c/sci_isletter.c \
@@ -64,7 +63,8 @@ GATEWAY_CPP_SOURCES = \
        sci_gateway/cpp/sci_regexp.cpp \
        sci_gateway/cpp/sci_part.cpp \
        sci_gateway/cpp/sci_length.cpp \
-       sci_gateway/cpp/sci_strindex.cpp
+       sci_gateway/cpp/sci_strindex.cpp \
+       sci_gateway/cpp/sci_strsubst.cpp
 
 GATEWAY_FORTRAN_SOURCES =  sci_gateway/fortran/sci_string.f \
 sci_gateway/fortran/strops.f
index 48aaee7..47656e2 100644 (file)
@@ -137,9 +137,8 @@ am__objects_4 = libscistring_la-gw_string.lo \
        libscistring_la-sci_convstr.lo libscistring_la-sci_emptystr.lo \
        libscistring_la-sci_str2code.lo libscistring_la-sci_strcat.lo \
        libscistring_la-sci_string.lo libscistring_la-sci_strsplit.lo \
-       libscistring_la-sci_strsubst.lo libscistring_la-sci_tokens.lo \
-       libscistring_la-sci_strcmp.lo libscistring_la-sci_isletter.lo \
-       libscistring_la-sci_isdigit.lo \
+       libscistring_la-sci_tokens.lo libscistring_la-sci_strcmp.lo \
+       libscistring_la-sci_isletter.lo libscistring_la-sci_isdigit.lo \
        libscistring_la-sci_isalphanum.lo \
        libscistring_la-sci_isascii.lo libscistring_la-sci_strchr.lo \
        libscistring_la-sci_strcspn.lo libscistring_la-sci_strncpy.lo \
@@ -150,7 +149,8 @@ am__objects_5 = sci_string.lo strops.lo
 am__objects_6 = libscistring_la-sci_stripblanks.lo \
        libscistring_la-sci_grep.lo libscistring_la-string_gw.lo \
        libscistring_la-sci_regexp.lo libscistring_la-sci_part.lo \
-       libscistring_la-sci_length.lo libscistring_la-sci_strindex.lo
+       libscistring_la-sci_length.lo libscistring_la-sci_strindex.lo \
+       libscistring_la-sci_strsubst.lo
 am_libscistring_la_OBJECTS = $(am__objects_1) $(am__objects_2) \
        $(am__objects_3) $(am__objects_4) $(am__objects_5) \
        $(am__objects_6)
@@ -479,7 +479,6 @@ sci_gateway/c/sci_str2code.c \
 sci_gateway/c/sci_strcat.c \
 sci_gateway/c/sci_string.c \
 sci_gateway/c/sci_strsplit.c \
-sci_gateway/c/sci_strsubst.c \
 sci_gateway/c/sci_tokens.c \
 sci_gateway/c/sci_strcmp.c \
 sci_gateway/c/sci_isletter.c \
@@ -503,7 +502,8 @@ GATEWAY_CPP_SOURCES = \
        sci_gateway/cpp/sci_regexp.cpp \
        sci_gateway/cpp/sci_part.cpp \
        sci_gateway/cpp/sci_length.cpp \
-       sci_gateway/cpp/sci_strindex.cpp
+       sci_gateway/cpp/sci_strindex.cpp \
+       sci_gateway/cpp/sci_strsubst.cpp
 
 GATEWAY_FORTRAN_SOURCES = sci_gateway/fortran/sci_string.f \
 sci_gateway/fortran/strops.f
@@ -999,13 +999,6 @@ libscistring_la-sci_strsplit.lo: sci_gateway/c/sci_strsplit.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -c -o libscistring_la-sci_strsplit.lo `test -f 'sci_gateway/c/sci_strsplit.c' || echo '$(srcdir)/'`sci_gateway/c/sci_strsplit.c
 
-libscistring_la-sci_strsubst.lo: sci_gateway/c/sci_strsubst.c
-@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -MT libscistring_la-sci_strsubst.lo -MD -MP -MF $(DEPDIR)/libscistring_la-sci_strsubst.Tpo -c -o libscistring_la-sci_strsubst.lo `test -f 'sci_gateway/c/sci_strsubst.c' || echo '$(srcdir)/'`sci_gateway/c/sci_strsubst.c
-@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscistring_la-sci_strsubst.Tpo $(DEPDIR)/libscistring_la-sci_strsubst.Plo
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='sci_gateway/c/sci_strsubst.c' object='libscistring_la-sci_strsubst.lo' libtool=yes @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -c -o libscistring_la-sci_strsubst.lo `test -f 'sci_gateway/c/sci_strsubst.c' || echo '$(srcdir)/'`sci_gateway/c/sci_strsubst.c
-
 libscistring_la-sci_tokens.lo: sci_gateway/c/sci_tokens.c
 @am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(libscistring_la_CFLAGS) $(CFLAGS) -MT libscistring_la-sci_tokens.lo -MD -MP -MF $(DEPDIR)/libscistring_la-sci_tokens.Tpo -c -o libscistring_la-sci_tokens.lo `test -f 'sci_gateway/c/sci_tokens.c' || echo '$(srcdir)/'`sci_gateway/c/sci_tokens.c
 @am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscistring_la-sci_tokens.Tpo $(DEPDIR)/libscistring_la-sci_tokens.Plo
@@ -1188,6 +1181,13 @@ libscistring_la-sci_strindex.lo: sci_gateway/cpp/sci_strindex.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o libscistring_la-sci_strindex.lo `test -f 'sci_gateway/cpp/sci_strindex.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_strindex.cpp
 
+libscistring_la-sci_strsubst.lo: sci_gateway/cpp/sci_strsubst.cpp
+@am__fastdepCXX_TRUE@  $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT libscistring_la-sci_strsubst.lo -MD -MP -MF $(DEPDIR)/libscistring_la-sci_strsubst.Tpo -c -o libscistring_la-sci_strsubst.lo `test -f 'sci_gateway/cpp/sci_strsubst.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_strsubst.cpp
+@am__fastdepCXX_TRUE@  $(am__mv) $(DEPDIR)/libscistring_la-sci_strsubst.Tpo $(DEPDIR)/libscistring_la-sci_strsubst.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     source='sci_gateway/cpp/sci_strsubst.cpp' object='libscistring_la-sci_strsubst.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(LIBTOOL)  --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscistring_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o libscistring_la-sci_strsubst.lo `test -f 'sci_gateway/cpp/sci_strsubst.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_strsubst.cpp
+
 .f.o:
        $(F77COMPILE) -c -o $@ $<
 
index 5cef1fe..9767945 100644 (file)
@@ -44,7 +44,6 @@ STRING_IMPEXP int sci_strtod(char *fname,unsigned long fname_len);
 //YaSp
 STRING_IMPEXP int sci_length(char *fname, int* _piKey);
 STRING_IMPEXP int sci_strsplit(char *fname, int* _piKey);
-STRING_IMPEXP int sci_strsubst(char *fname, int* _piKey);
 STRING_IMPEXP int sci_isdigit(char *fname, int* _piKey);
 STRING_IMPEXP int sci_isalphanum(char *fname, int* _piKey);
 STRING_IMPEXP int sci_isascii(char *fname, int* _piKey);
index 8ad4799..ef591b9 100644 (file)
@@ -33,6 +33,7 @@ CPP_GATEWAY_PROTOTYPE(sci_regexp);
 CPP_GATEWAY_PROTOTYPE(sci_part);
 CPP_GATEWAY_PROTOTYPE(sci_length);
 CPP_GATEWAY_PROTOTYPE(sci_strindex);
+CPP_GATEWAY_PROTOTYPE(sci_strsubst);
 
 
 #endif /* !__STRING_GW_HXX__ */
index 03ccc6a..01587a9 100644 (file)
@@ -39,4 +39,11 @@ STRING_IMPEXP char **strsubst_reg(char **strings_input,int strings_dim,char *str
 STRING_IMPEXP char *strsub(char* input_string, const char* string_to_search, const char* replacement_string);
 STRING_IMPEXP char *strsub_reg(char* input_string, const char* string_to_search, const char* replacement_string, int *ierr);
 
+
+STRING_IMPEXP wchar_t **wcssubst(wchar_t** _pwstInput, int _iInputSize, wchar_t* _pwstSearch, wchar_t* _pwstReplace);
+STRING_IMPEXP wchar_t **wcssubst_reg(wchar_t** _pwstInput, int _iInputSize, wchar_t* _pwstSearch, wchar_t* _pwstReplace, int* _piErr);
+
+STRING_IMPEXP wchar_t *wcssub(wchar_t* _pwstInput, wchar_t* _pwstSearch, wchar_t* _pwstReplace);
+STRING_IMPEXP wchar_t *wcssub_reg(wchar_t* _pwstInput, wchar_t* _pwstSearch, wchar_t* _pwstReplace, int* _piErr);
+
 #endif /* __STRSUBST_H__ */
diff --git a/scilab/modules/string/sci_gateway/c/sci_strsubst.c b/scilab/modules/string/sci_gateway/c/sci_strsubst.c
deleted file mode 100644 (file)
index 6f9b51c..0000000
+++ /dev/null
@@ -1,418 +0,0 @@
-/*
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) INRIA - Cong WU , Allan CORNET
- * Copyright (C) DIGITEO - 2009 - Allan CORNET
- *
- * This file must be used under the terms of the CeCILL.
- * This source file is licensed as described in the file COPYING, which
- * you should have received as part of this distribution.  The terms
- * are also available at
- * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
- *
- */
-/*------------------------------------------------------------------------*/
-#include <string.h>
-#include "gw_string.h"
-#include "stack-c.h"
-#include "MALLOC.h"
-#include "api_scilab.h"
-#include "freeArrayOfString.h"
-#include "strsubst.h"
-#include "localization.h"
-#include "Scierror.h"
-#include "pcre_error.h"
-#include "pcre_private.h"
-#include "BOOL.h"
-/*-------------------------------------------------------------------------------------*/
-#define CHAR_R "r"
-#define CHAR_S "s"
-/*-------------------------------------------------------------------------------------*/
-int sci_strsubst(char *fname, int* _piKey)
-{
-       SciErr sciErr;
-       int *piAddressVarOne    = NULL;
-       int iType                                                       = 0;
-       int mOne        = 0, nOne               = 0;
-
-       CheckRhs(3,4);
-       CheckLhs(1,1);
-
-       sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
-       if(sciErr.iErr)
-       {
-               printError(&sciErr, 0);
-               return 0;
-       }
-       sciErr = getVarType(_piKey, piAddressVarOne, &iType);
-       if(sciErr.iErr)
-       {
-               printError(&sciErr, 0);
-               return 0;
-       }
-
-       if (iType == sci_strings)
-       {
-               char **pStVarOne                        = NULL;
-               int *lenStVarOne                        = NULL;
-
-               int *piAddressVarTwo    = NULL;
-               int iType2                                              = 0;
-               int mTwo = 0, nTwo              = 0;
-               char *pStVarTwo                         = NULL;
-               int lenStVarTwo                         = 0;
-
-               int *piAddressVarThree = NULL;
-               int mThree = 0, nThree = 0;
-               char *pStVarThree                               = NULL;
-               int lenStVarThree                               = 0;
-               int iType3                                                      = 0;
-
-               BOOL isRegExp = FALSE;
-
-               if (Rhs == 4)
-               {
-                       int *piAddressVarFour = NULL;
-                       int iType4                                              = 0;
-                       int mFour = 0, nFour    = 0;
-                       char *pStVarFour                        = NULL;
-                       int lenStVarFour                        = 0;
-
-                       sciErr = getVarAddressFromPosition(_piKey, 4, &piAddressVarFour);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       sciErr = getVarType(_piKey, piAddressVarFour, &iType4);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       if (iType4  != sci_strings )
-                       {
-                               Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,4);
-                               return 0;
-                       }
-
-                       sciErr = getMatrixOfString(_piKey, piAddressVarFour,&mFour,&nFour,&lenStVarFour,&pStVarFour);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       if ( (mFour != nFour) && (nFour != 1) ) 
-                       {
-                               Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,4);
-                               return 0;
-                       }
-
-                       pStVarFour = (char*)MALLOC(sizeof(char)*(lenStVarFour + 1));
-                       if (pStVarFour)
-                       {
-                               sciErr = getMatrixOfString(_piKey, piAddressVarFour, &mFour, &nFour, &lenStVarFour, &pStVarFour);
-                               if(sciErr.iErr)
-                               {
-                                       printError(&sciErr, 0);
-                                       return 0;
-                               }
-
-                               if ( (strcmp(pStVarFour,CHAR_R) == 0) || (strcmp(pStVarFour,CHAR_S) == 0) )
-                               {
-                                       if (strcmp(pStVarFour,CHAR_R) == 0)
-                                       {
-                                               isRegExp = TRUE;
-                                       }
-                                       else
-                                       {
-                                               isRegExp = FALSE;
-                                       }
-                                       FREE(pStVarFour); pStVarFour = NULL;
-                               }
-                               else
-                               {
-                                       FREE(pStVarFour); pStVarFour = NULL;
-                                       Scierror(999,_("%s: Wrong value for input argument #%d: 's' or 'r' expected.\n"),fname,4);
-                                       return 0;
-                               }
-                       }
-                       else
-                       {
-                               Scierror(999,_("%s: No more memory.\n"),fname);
-                               return 0;
-                       }
-               }
-
-               sciErr = getVarAddressFromPosition(_piKey, 3, &piAddressVarThree);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               // checks type 3th input argument
-               sciErr = getVarType(_piKey, piAddressVarThree, &iType3);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               if (iType3  != sci_strings )
-               {
-                       Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,3);
-                       return 0;
-               }
-
-               // checks dimension 3th input argument
-               sciErr = getMatrixOfString(_piKey, piAddressVarThree,&mThree,&nThree,&lenStVarThree,&pStVarThree);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               if ( (mThree != nThree) && (nThree != 1) ) 
-               {
-                       Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,3);
-                       return 0;
-               }
-
-               sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-
-               // checks type 2nd input argument
-               sciErr = getVarType(_piKey, piAddressVarTwo, &iType2);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               if (iType2  != sci_strings )
-               {
-                       Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
-                       return 0;
-               }
-
-               // checks dimension 2nd input argument
-               sciErr = getMatrixOfString(_piKey, piAddressVarTwo,&mTwo,&nTwo,&lenStVarTwo,&pStVarTwo);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               if ( (mTwo != nTwo) && (nTwo != 1) ) 
-               {
-                       Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
-                       return 0;
-               }
-
-               sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               // checks type 1st input argument
-               sciErr = getVarType(_piKey, piAddressVarOne, &iType);
-               if (iType  != sci_strings )
-               {
-                       Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
-                       return 0;
-               }
-
-               // get length 3th input argument
-               sciErr = getMatrixOfString(_piKey, piAddressVarThree, &mThree, &nThree, &lenStVarThree, &pStVarThree);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               pStVarThree = (char*)MALLOC(sizeof(char)*(lenStVarThree + 1));
-               if (pStVarThree)
-               {
-                       // get value 3th input argument
-                       sciErr = getMatrixOfString(_piKey, piAddressVarThree, &mThree, &nThree, &lenStVarThree, &pStVarThree);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       // get length 2nd input argument
-                       sciErr = getMatrixOfString(_piKey, piAddressVarTwo, &mTwo, &nTwo, &lenStVarTwo, &pStVarTwo);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       pStVarTwo = (char*)MALLOC(sizeof(char)*(lenStVarTwo + 1));
-                       if (pStVarTwo)
-                       {
-                               // get value 2nd input argument
-                               sciErr = getMatrixOfString(_piKey, piAddressVarTwo, &mTwo, &nTwo, &lenStVarTwo, &pStVarTwo);
-                               if(sciErr.iErr)
-                               {
-                                       printError(&sciErr, 0);
-                                       return 0;
-                               }
-
-                               sciErr = getMatrixOfString(_piKey, piAddressVarOne,&mOne,&nOne,lenStVarOne,pStVarOne);
-                               if(sciErr.iErr)
-                               {
-                                       printError(&sciErr, 0);
-                                       return 0;
-                               }
-
-                               lenStVarOne = (int *)MALLOC(sizeof(int) * (mOne * nOne));
-                               if (lenStVarOne)
-                               {
-                                       sciErr = getMatrixOfString(_piKey, piAddressVarOne,&mOne,&nOne,lenStVarOne,pStVarOne);
-                                       if(sciErr.iErr)
-                                       {
-                                               printError(&sciErr, 0);
-                                               return 0;
-                                       }
-
-                                       pStVarOne = (char **)MALLOC(sizeof(char *) * (mOne * nOne));
-                                       if (pStVarOne)
-                                       {
-                                               char **Output_StringMatrix = NULL;
-
-                                               int i = 0;
-                                               for (i = 0; i < mOne * nOne; i++)
-                                               {
-                                                       pStVarOne[i] = (char*)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
-                                                       if (pStVarOne[i] == NULL)
-                                                       {
-                                                               FREE(pStVarThree); pStVarThree = NULL;
-                                                               FREE(pStVarTwo); pStVarTwo = NULL;
-                                                               FREE(lenStVarOne); lenStVarOne = NULL;
-                                                               freeArrayOfString(pStVarOne, i);
-                                                               Scierror(999,_("%s: No more memory.\n"), fname);
-                                                               return 0;
-                                                       }
-                                               }
-
-                                               sciErr = getMatrixOfString(_piKey, piAddressVarOne,&mOne,&nOne,lenStVarOne,pStVarOne);
-                                               if(sciErr.iErr)
-                                               {
-                                                       printError(&sciErr, 0);
-                                                       return 0;
-                                               }
-
-
-                                               FREE(lenStVarOne); lenStVarOne = NULL;
-
-                                               if (isRegExp)
-                                               {
-                                                       int ierr = (int)PCRE_FINISHED_OK;
-                                                       Output_StringMatrix = strsubst_reg(pStVarOne, mOne * nOne, pStVarTwo, pStVarThree, &ierr);
-                                                       if ( (ierr != PCRE_FINISHED_OK) && (ierr != NO_MATCH) && (ierr != PCRE_EXIT) )
-                                                       {
-                                                               FREE(pStVarThree); pStVarThree = NULL;
-                                                               FREE(pStVarTwo); pStVarTwo = NULL;
-                                                               pcre_error(fname, ierr);
-                                                               return 0;
-                                                       }
-                                               }
-                                               else
-                                               {
-                                                       Output_StringMatrix = strsubst(pStVarOne, mOne * nOne, pStVarTwo, pStVarThree);
-                                               }
-
-                                               FREE(pStVarThree); pStVarThree = NULL;
-                                               FREE(pStVarTwo); pStVarTwo = NULL;
-                                               freeArrayOfString(pStVarOne, mOne* nOne);
-
-                                               sciErr = createMatrixOfString(_piKey, Rhs + 1, mOne, nOne, Output_StringMatrix);
-                                               freeArrayOfString(Output_StringMatrix,mOne * nOne);
-                                               if(sciErr.iErr)
-                                               {
-                                                       printError(&sciErr, 0);
-                                                       return 0;
-                                               }
-
-                                               LhsVar(1) = Rhs + 1 ;
-                                               C2F(putlhsvar)();
-                                               return 0;
-                                       }
-                                       else
-                                       {
-                                               FREE(pStVarThree); pStVarThree = NULL;
-                                               FREE(pStVarTwo); pStVarTwo = NULL;
-                                               FREE(lenStVarOne); lenStVarOne = NULL;
-                                               Scierror(999,_("%s: No more memory.\n"),fname);
-                                               return 0;
-                                       }
-                               }
-                               else
-                               {
-                                       FREE(pStVarThree); pStVarThree = NULL;
-                                       FREE(pStVarTwo); pStVarTwo = NULL;
-                                       Scierror(999,_("%s: No more memory.\n"),fname);
-                                       return 0;
-                               }
-                       }
-                       else
-                       {
-                               FREE(pStVarThree); pStVarThree = NULL;
-                               Scierror(999,_("%s: No more memory.\n"),fname);
-                               return 0;
-                       }
-               }
-               else
-               {
-                       Scierror(999,_("%s: No more memory.\n"),fname);
-                       return 0;
-               }
-       }
-       else if (iType == sci_matrix)
-       {
-               sciErr = getVarDimension(_piKey, piAddressVarOne, &mOne, &nOne);
-               if(sciErr.iErr)
-               {
-                       printError(&sciErr, 0);
-                       return 0;
-               }
-
-               if ( (mOne == 0) && (nOne == 0) ) /* strsubst([],...) returns [] */
-               {
-                       sciErr = createMatrixOfDouble(_piKey, Rhs + 1, mOne, nOne, NULL);
-                       if(sciErr.iErr)
-                       {
-                               printError(&sciErr, 0);
-                               return 0;
-                       }
-
-                       LhsVar(1) = Rhs + 1 ;
-                       C2F(putlhsvar)();
-                       return 0;
-               }
-               else
-               {
-                       Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"), fname);
-                       return 0;
-               }
-       }
-       else
-       {
-               Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
-               return 0;
-       }
-       return 0;
-}
-/*-------------------------------------------------------------------------------------*/
index 1a7ddb3..c3eef25 100644 (file)
@@ -40,9 +40,6 @@ Function::ReturnValue sci_regexp(typed_list &in, int _iRetCount, typed_list &out
     wchar_t* pwstInput      = NULL;
     wchar_t* pwstPattern    = NULL;
 
-    char* pstInput          = NULL;
-    char* pstPattern        = NULL;
-
     int iPcreStatus         = 0;
     int iStart              = 0;
     int iStep               = 0;
@@ -94,9 +91,6 @@ Function::ReturnValue sci_regexp(typed_list &in, int _iRetCount, typed_list &out
         wcType = WSTR_ONCE;
     }
 
-    pstInput    = wide_string_to_UTF8(pwstInput);
-    pstPattern  = wide_string_to_UTF8(pwstPattern);
-
     //input is empty
     if(wcslen(pwstInput) == 0)
     {
@@ -121,35 +115,22 @@ Function::ReturnValue sci_regexp(typed_list &in, int _iRetCount, typed_list &out
 
     do
     {
-        iPcreStatus = pcre_private(pstInput + iStep, pstPattern, &iStart, &iEnd);
+        iPcreStatus = wide_pcre_private(pwstInput + iStep, pwstPattern, &iStart, &iEnd);
         if(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd)
         {
-            char* pstTempStart  = os_strdup(pstInput);
-            pstTempStart[iStart + iStep] = 0;
-
-            char* pstTempEnd    = os_strdup(pstInput);
-            pstTempEnd[iEnd + iStep]     = 0;
-
-            wchar_t* pwstTempStart  = to_wide_string(pstTempStart);
-            wchar_t* pwstTempEnd    = to_wide_string(pstTempEnd);
-
-
-            piStart[iOccurs] = static_cast<int>(wcslen(pwstTempStart));
-            piEnd[iOccurs]   = static_cast<int>(wcslen(pwstTempEnd));
-
-            FREE(pstTempStart);
-            FREE(pstTempEnd);
-            FREE(pwstTempStart);
-            FREE(pwstTempEnd);
-            //piStart[iOccurs] = iStart + iStep;
-            //piEnd[iOccurs]   = iEnd + iStep;
-            iOccurs++;
-            iStep += iEnd;
+            piStart[iOccurs]    = iStart + iStep;
+            piEnd[iOccurs++]    = iEnd + iStep;
+            iStep               += iEnd;
+        }
+        else if(iPcreStatus != NO_MATCH)
+        {
+            pcre_error("regexp", iPcreStatus);
+            delete[] piStart;
+            delete[] piEnd;
+            return Function::Error;
         }
     }while(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd && wcType != WSTR_ONCE);
  
-    FREE(pstInput);
-    FREE(pstPattern);
 
     Double* pStart = new Double(1, iOccurs);
     double* pdblStart = pStart->real_get();
@@ -174,311 +155,28 @@ Function::ReturnValue sci_regexp(typed_list &in, int _iRetCount, typed_list &out
 
     if(_iRetCount == 3)
     {
-        String *pS = new String(iOccurs, 1);
-        for(int i = 0 ; i < iOccurs ; i++)
+        String *pS = NULL;
+        if(iOccurs == 0)
+        {
+            pS = new String(1,1);
+            pS->string_set(0, L"");
+        }
+        else
         {
-            wchar_t* pwstTemp = new wchar_t[piEnd[i] - piStart[i] + 1];
-            wcsncpy(pwstTemp, pwstInput + piStart[i], piEnd[i] - piStart[i]);
-            pwstTemp[piEnd[i] - piStart[i]] = 0;
-            pS->string_set(i, 0, pwstTemp);
-            delete[] pwstTemp;
+            pS = new String(iOccurs, 1);
+            for(int i = 0 ; i < iOccurs ; i++)
+            {
+                wchar_t* pwstTemp = new wchar_t[piEnd[i] - piStart[i] + 1];
+                wcsncpy(pwstTemp, pwstInput + piStart[i], piEnd[i] - piStart[i]);
+                pwstTemp[piEnd[i] - piStart[i]] = 0;
+                pS->string_set(i, 0, pwstTemp);
+                delete[] pwstTemp;
+            }
         }
         out.push_back(pS);
     }
     delete[] piStart;
     delete[] piEnd;
     return Function::OK;
-       //char typ = CHAR_S;
-       //char **Str = NULL;
-       //char **Str2 = NULL;
-       //int i = 0; /* loop indice */
-
-       //int mn = 0; /* dimension parameter 1 m*n */
-
-       //int m2 = 0,n2 = 0;
-       //int mn2 = 0; /* m2*n2 */
-
-       //int outIndex = 0;
-       //int numRow = 1;
-
-       //int *values_start = NULL;
-       //int *values_end = NULL;
-
-       //int *wcvalues_start = NULL;
-       //int *wcvalues_end = NULL;
-
-
-       //int nbValues_start = 0;
-       //int nbValues_end=0;
-       //int nbposition = 0;
-
-       //CheckRhs(2,3);
-       //CheckLhs(1,3);
-
-       //if (VarType(1) == sci_strings)
-       //{
-       //      int m1 = 0;
-       //      int n1 = 0;
-
-       //      GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Str);
-       //      mn = m1*n1;
-       //}
-       //else
-       //{
-       //      ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), fname,1);
-       //      return 0;
-       //}
-
-       //if (mn != 1)
-       //{
-       //      freeArrayOfString(Str,mn);
-       //      ScierrorW(36, _W("%ls: Wrong size for input argument #%d: Single string expected.\n"), fname,1);
-       //      return 0;
-       //}
-
-       //if (VarType(2) == sci_strings)
-       //{
-       //      int m2 = 0,n2 = 0;
-       //      GetRhsVar(2,MATRIX_OF_STRING_DATATYPE,&m2,&n2,&Str2);
-       //      mn2 = m2*n2;
-       //}
-       //else
-       //{
-       //      freeArrayOfString(Str,mn);
-       //      ScierrorW(36, _W("%ls: Wrong type for input argument #%d: Single string expected.\n"), fname,2);
-       //      return 0;
-       //}
-
-       //if (mn2 != 1)
-       //{
-       //      freeArrayOfString(Str2,mn2);
-       //      ScierrorW(36, _W("%ls: Wrong size for input argument #%d: Single string expected.\n"), fname,2);
-       //      return 0;
-       //}
-
-       //if ( (int)strlen(Str[0]) == 0 )
-       //{
-       //      values_start = (int *)MALLOC(sizeof(int));
-       //      values_end = (int *)MALLOC(sizeof(int));
-
-       //      wcvalues_start = (int *)MALLOC(sizeof(int));
-       //      wcvalues_end = (int *)MALLOC(sizeof(int));
-       //}
-       //else
-       //{
-       //      values_start = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
-       //      values_end = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
-
-       //      wcvalues_start = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
-       //      wcvalues_end = (int *)MALLOC( sizeof(int) * ( strlen(Str[0]) ) );
-       //}
-
-       //if (Rhs == 2 )
-       //{
-       //      typ = CHAR_S;
-       //}
-       //else /* Rhs == 3 */
-       //{
-       //      int m3 = 0, n3 = 0, l3 = 0;
-
-       //      GetRhsVar(3,STRING_DATATYPE,&m3,&n3,&l3);
-
-       //      if ( m3*n3 != 0) typ = cstk(l3)[0];
-       //      if (typ != STR_ONCE)
-       //      {
-       //              freeArrayOfString(Str,mn);
-       //              freeArrayOfString(Str2,mn2);
-       //              ScierrorW(999,_W("%ls: Wrong type for input argument #%d: '%s' expected.\n"),fname,3,"o");
-       //              return 0;
-       //      }
-       //}
-
-       //{
-       //      int x = 0;
-       //      pcre_error_code answer = PCRE_FINISHED_OK;
-
-       //      int start_point = 0;
-       //      int Output_Start = 0;
-       //      int Output_End = 0;
-
-       //      int wcOutput_Start = 0;
-       //      int wcstart_point = 0;
-       //      int wcOutput_End = 0;
-
-       //      for (x = 0; x < mn2; ++x)
-       //      {
-       //              char *save = os_strdup(Str2[x]);
-       //              if (save)
-       //              {
-       //                      char *pointer = Str[0];
-       //                      start_point = 0;
-       //                      wcstart_point = 0;
-       //                      do
-       //                      {
-       //                              strcpy(save,Str2[x]);
-       //                              Output_Start = 0;
-       //                              Output_End = 0;
-
-       //                              answer = pcre_private(pointer,save,&Output_Start,&Output_End);
-       //                              if ( answer == PCRE_FINISHED_OK )
-       //                              {
-       //                                      /* Start = End means that we matched a position and 0 characters.
-       //                                      * Matching 0 characters, for us, means no match.
-       //                                      */
-       //                                      if (Output_Start != Output_End)
-       //                                      {
-       //                                              char *  strOutput_Start = os_strdup(pointer);
-       //                                              char *  strOutput_End =  os_strdup(pointer);
-
-       //                                              wchar_t *wcstrOutput_Start = NULL;
-       //                                              wchar_t *wcstrOutput_End = NULL;
-
-       //                                              /* calculates positions with wide characters */
-       //                                              strOutput_Start[Output_Start] = '\0';
-       //                                              strOutput_End[Output_End] = '\0';
-
-       //                                              wcstrOutput_Start = to_wide_string(strOutput_Start);
-       //                                              wcstrOutput_End = to_wide_string(strOutput_End);
-
-       //                                              if (wcstrOutput_Start)
-       //                                              {
-       //                                                      wcOutput_Start = (int)wcslen(wcstrOutput_Start);
-       //                                                      FREE(wcstrOutput_Start);wcstrOutput_Start = NULL;
-       //                                              }
-       //                                              else
-       //                                              {
-       //                                                      wcOutput_Start = 0;
-       //                                              }
-
-       //                                              if (wcstrOutput_End)
-       //                                              {
-       //                                                      wcOutput_End = (int)wcslen(wcstrOutput_End);
-       //                                                      FREE(wcstrOutput_End);wcstrOutput_End = NULL;
-       //                                              }
-       //                                              else
-       //                                              {
-       //                                                      wcOutput_End = 0;
-       //                                              }
-
-       //                                              if (strOutput_Start) {FREE(strOutput_Start); strOutput_Start = NULL;}
-       //                                              if (strOutput_End) {FREE(strOutput_End); strOutput_End = NULL;}
-
-       //                                              /*adding the answer into the outputmatrix*/
-       //                                              values_start[nbValues_start] = Output_Start + start_point + 1;
-       //                                              values_end[nbValues_end] = Output_End + start_point;
-
-       //                                              wcvalues_start[nbValues_start] = wcOutput_Start + wcstart_point + 1;
-       //                                              wcvalues_end[nbValues_end] = wcOutput_End + wcstart_point;
-
-       //                                              nbValues_start++;
-       //                                              nbValues_end++;
-
-       //                                              /*The number according to the str2 matrix*/
-       //                                              nbposition++;
-       //                                      }
-       //                                      else if(Output_End == 0 && *pointer != '\0')
-       //                                      {
-       //                                              /* Avoid an infinite loop */
-       //                                              pointer++;
-       //                                      }
-
-       //                                      pointer = &pointer[Output_End];
-       //                                      start_point = start_point + Output_End ;
-       //                                      wcstart_point = wcstart_point + wcOutput_End ;
-       //                              }
-       //                              else
-       //                              {
-       //                                      if (answer != NO_MATCH)
-       //                                      {
-       //                                              pcre_error(fname,answer);
-       //                                              freeArrayOfString(Str,mn);
-       //                                              freeArrayOfString(Str2,mn2);
-       //                                              return 0;
-       //                                      }
-       //                              }
-       //                      }
-       //                      while( (answer == PCRE_FINISHED_OK) && (*pointer != '\0') &&  (typ != STR_ONCE) );
-
-       //                      if (save) {FREE(save);save=NULL;}
-       //              }
-       //              else
-       //              {
-       //                      freeArrayOfString(Str,mn);
-       //                      freeArrayOfString(Str2,mn2);
-       //                      ScierrorW(999, _W("%ls: No more memory.\n"),fname);
-       //                      return 0;
-       //              }
-       //      }
-       //}
-
-       //numRow   = 1; /* Output values[] */
-       //outIndex = 0;
-       //CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues_start,&outIndex);
-       //for ( i = 0 ; i < nbValues_start ; i++ )
-       //{
-       //      stk(outIndex)[i] = (double)wcvalues_start[i] ;
-       //}
-       //LhsVar(1) = Rhs+1 ;
-
-       //if (Lhs >= 2)
-       //{
-       //      numRow   = 1;
-       //      outIndex = 0;
-       //      CreateVar(Rhs+2,MATRIX_OF_DOUBLE_DATATYPE,&numRow,&nbValues_end,&outIndex);
-       //      for ( i = 0 ; i < nbposition ; i++ )
-       //      {
-       //              stk(outIndex)[i] = (double)wcvalues_end[i] ;
-       //      }
-       //      LhsVar(2) = Rhs+2;
-       //}
-
-       //if (Lhs == 3)
-       //{
-       //      if (nbValues_start != 0)
-       //      {
-       //              char **match = (char**)MALLOC(sizeof(char*)*(nbValues_start));
-
-       //              if (match == NULL)
-       //              {
-       //                      freeArrayOfString(Str, mn);
-       //                      freeArrayOfString(Str2, mn2);
-
-       //                      ScierrorW(999, _W("%ls: No more memory.\n"),fname);
-       //                      return 0;
-       //              }
-
-       //              for( i = 0; i < nbValues_start; i++)
-       //              {
-       //                      int len = values_end[i] - values_start[i] + 1;
-       //                      match[i] = (char*)MALLOC(sizeof(char)*(len + 1));
-       //                      strncpy(match[i], Str[0] + values_start[i] - 1, len);
-
-       //                      /* A char* always finished by \0 */
-       //                      match[i][len] = '\0';
-       //              }
-
-       //              numRow =  nbValues_start;
-       //              outIndex = 1 ;
-       //              CreateVarFromPtr(Rhs + 3,MATRIX_OF_STRING_DATATYPE, &numRow, &outIndex, match );
-       //              LhsVar(3) = Rhs + 3 ;
-       //              freeArrayOfString(match,nbValues_start);
-       //      }
-       //      else
-       //      {
-       //              int m3 = 0, n3 = 0, l3 = 0;
-       //              CreateVar(Rhs+3,STRING_DATATYPE,  &m3, &n3, &l3);
-       //              LhsVar(3) = Rhs + 3 ;
-       //      }
-       //}
-
-       //freeArrayOfString(Str, mn);
-       //freeArrayOfString(Str2, mn2);
-       //if (values_start) {FREE(values_start); values_start = NULL;}
-       //if (values_end) {FREE(values_end); values_end = NULL;}
-       //if (wcvalues_start) {FREE(wcvalues_start); wcvalues_start = NULL;}
-       //if (wcvalues_end) {FREE(wcvalues_end); wcvalues_end = NULL;}
-
-       //C2F(putlhsvar)();
-       //return 0;
 }
 /*-----------------------------------------------------------------------------------*/
index d44e6d0..eab115d 100644 (file)
@@ -103,45 +103,35 @@ types::Function::ReturnValue sci_strindex(types::typed_list &in, int _iRetCount,
     int iValues = 0;
     if(bRegExp)
     {//pcre
-        pcre_error_code errorCode = PCRE_FINISHED_OK;
-
-       //              int Output_Start = 0;
-       //              int Output_End = 0;
-
-       //              /* We use pcre library */
-        char* pstIn = wide_string_to_UTF8(pwstData);
+        pcre_error_code iPcreStatus = PCRE_FINISHED_OK;
         for(int i = 0 ; i < pS->size_get() ; i++)
         {
             int iStart      = 0;
             int iEnd        = 0;
-            char* pstSearch = wide_string_to_UTF8(pwstSearch[i]);
-
-            errorCode = pcre_private(pstIn, pstSearch, &iStart,&iEnd);
-
-            if ( errorCode == PCRE_FINISHED_OK)
-                       {
-                pstSearch[iStart]               = L'\0';
-                wchar_t* pstTemp                = to_wide_string(pstSearch);
-                pstrResult[iValues].data        = (int)wcslen(pstTemp) + 1;
-                pstrResult[iValues].position    = i + 1;
-                iValues++;
-                FREE(pstTemp);
-            }
-            else
+            int iStep       = 0;
+
+            do
             {
-                if (errorCode != NO_MATCH)
-                {
-                    pcre_error("strindex", errorCode);
-                    FREE(pstSearch);
-                    FREE(pstIn);
-                    delete[] pstrResult;
-                    return Function::Error;
+                iPcreStatus = wide_pcre_private(pwstData + iStep, pwstSearch[i], &iStart,&iEnd);
+                if(iPcreStatus == PCRE_FINISHED_OK)
+                           {
+                    pstrResult[iValues].data        = iStart + iStep + 1;
+                    pstrResult[iValues].position    = i + 1;
+                    iStep                           += iEnd;
+                    iValues++;
                 }
+                else
+                {
+                    if(iPcreStatus != NO_MATCH)
+                    {
+                        pcre_error("strindex", iPcreStatus);
+                        delete[] pstrResult;
+                        return Function::Error;
+                    }
                     break;
-            }
-            FREE(pstSearch);
+                }
+            }while(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd);
         }
-        FREE(pstIn);
     }
     else
     {
diff --git a/scilab/modules/string/sci_gateway/cpp/sci_strsubst.cpp b/scilab/modules/string/sci_gateway/cpp/sci_strsubst.cpp
new file mode 100644 (file)
index 0000000..cdba29c
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) INRIA - Cong WU , Allan CORNET
+ * Copyright (C) DIGITEO - 2009 - Allan CORNET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+/*------------------------------------------------------------------------*/
+#include "funcmanager.hxx"
+#include "string_gw.hxx"
+
+extern "C"
+{
+#include "strsubst.h"
+#include "localization.h"
+#include "Scierror.h"
+#include "pcre_error.h"
+#include "pcre_private.h"
+#include "freeArrayOfString.h"
+}
+/*-------------------------------------------------------------------------------------*/
+#define WCHAR_R L'r'
+#define WCHAR_S L's'
+/*-------------------------------------------------------------------------------------*/
+types::Function::ReturnValue sci_strsubst(types::typed_list &in, int _iRetCount, types::typed_list &out)
+{
+    bool bRegExp = false;
+    if(in.size() < 3 || in.size() > 4)
+    {
+        ScierrorW(77, _W("%ls: Wrong number of input argument(s): %d to %d expected.\n"), L"strsubst", 3, 4);
+        return Function::Error;
+    }
+    
+    if(in.size() > 3)
+    {
+        if(in[3]->isString() == false && in[3]->getAsString()->size_get() != 1)
+        {
+            ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A string expected.\n"), L"strsubst", 4);
+            return Function::Error;
+        }
+
+        if(in[3]->getAsString()->string_get(0)[0] == WCHAR_R)
+        {
+            bRegExp = true;
+        }
+        else if(in[3]->getAsString()->string_get(0)[0] == WCHAR_S)
+        {
+            bRegExp = false;
+        }
+        else
+        {
+            ScierrorW(999, _W("%ls: Wrong value for input argument #%d: 's' or 'r' expected.\n"), L"strsubst", 4);
+            return Function::Error;
+        }
+    }
+
+    if(in[2]->isString() == false || in[2]->getAsString()->size_get() != 1)
+    {
+        ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A string expected.\n"), L"strsubst", 3);
+        return Function::Error;
+    }
+
+    wchar_t* pwstReplace = in[2]->getAsString()->string_get()[0];
+
+    if(in[1]->isString() == false || in[1]->getAsString()->size_get() != 1)
+    {
+        ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A string expected.\n"), L"strsubst", 2);
+        return Function::Error;
+    }
+
+    wchar_t* pwstSearch = in[1]->getAsString()->string_get()[0];
+
+    if(in[0]->isDouble() && in[0]->getAsDouble()->isEmpty())
+    {
+        out.push_back(Double::Empty());
+        return Function::OK;
+    }
+
+    if(in[0]->isString() == false)
+    {
+        ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A string matrix expected.\n"), L"strsubst", 1);
+        return Function::Error;
+    }
+
+    String* pS = in[0]->getAsString();
+
+    String* pOut = new String(pS->rows_get(), pS->cols_get());
+    wchar_t** pwstOutput = NULL;
+
+    if(bRegExp)
+    {
+        int iErr = 0;
+        pwstOutput = wcssubst_reg(pS->string_get(), pS->size_get(), pwstSearch, pwstReplace, &iErr);
+        if(iErr != NO_MATCH)
+        {
+            pcre_error("strsubst", iErr);
+            delete pOut;
+            return Function::Error;
+        }
+    }
+    else
+    {
+        pwstOutput = wcssubst(pS->string_get(), pS->size_get(), pwstSearch, pwstReplace);
+    }
+
+    pOut->string_set(pwstOutput);
+    freeArrayOfWideString(pwstOutput, pOut->size_get());
+    out.push_back(pOut);
+    return Function::OK;
+}
+/*-------------------------------------------------------------------------------------*/
index bf482d0..cfbc935 100644 (file)
@@ -22,5 +22,6 @@ bool StringModule::Load()
     symbol::Context::getInstance()->AddFunction(Function::createFunction(L"part", &sci_part, MODULE_NAME));
     symbol::Context::getInstance()->AddFunction(Function::createFunction(L"length", &sci_length, MODULE_NAME));
     symbol::Context::getInstance()->AddFunction(Function::createFunction(L"strindex", &sci_strindex, MODULE_NAME));
+    symbol::Context::getInstance()->AddFunction(Function::createFunction(L"strsubst", &sci_strsubst, MODULE_NAME));
     return true;
 }
index 1b31856..779fddd 100644 (file)
                                >
                        </File>
                        <File
+                               RelativePath=".\sci_strsubt.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\string_gw.cpp"
                                >
                        </File>
index ee3604f..550c39f 100644 (file)
@@ -27,6 +27,7 @@
 #include "strsubst.h"
 #include "sci_warning.h"
 #include "sciprint.h"
+#include "charEncoding.h"
 /*-------------------------------------------------------------------------------*/
 /* A number of things vary for Windows builds. Originally, pcretest opened its
 input and output without "b"; then I was told that "b" was needed in some
@@ -868,5 +869,46 @@ SKIP_DATA:
 
        return PCRE_EXIT;
 }
-
-/*-------------------------------------------------------------------------------*/
\ No newline at end of file
+/*-------------------------------------------------------------------------------*/
+pcre_error_code wide_pcre_private(wchar_t* _pwstInput, wchar_t* _pwstPattern, int* _piStart, int* _piEnd)
+{
+    int iStart          = 0;
+    int iEnd            = 0;
+    int iPcreStatus     = 0;
+
+    char* pstInput      = wide_string_to_UTF8(_pwstInput);
+    char* pstPattern    = wide_string_to_UTF8(_pwstPattern);
+
+
+    iPcreStatus = pcre_private(pstInput, pstPattern, &iStart, &iEnd);
+    if(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd)
+    {
+        char* pstTempStart      = NULL;
+        char* pstTempEnd        = NULL;
+        wchar_t* pwstTempStart  = NULL;
+        wchar_t* pwstTempEnd    = NULL;
+
+        pstTempStart            = os_strdup(pstInput);
+        pstTempEnd              = os_strdup(pstInput);
+        pstTempEnd[iEnd]        = 0;
+        pstTempStart[iStart]    = 0;
+
+
+        pwstTempStart           = to_wide_string(pstTempStart);
+        pwstTempEnd             = to_wide_string(pstTempEnd);
+
+        *_piStart               = (int)wcslen(pwstTempStart);
+        *_piEnd                 = (int)wcslen(pwstTempEnd);
+
+        FREE(pstTempStart);
+        FREE(pstTempEnd);
+        FREE(pwstTempStart);
+        FREE(pwstTempEnd);
+    }
+    else
+    {
+        *_piStart   = iStart;
+        *_piEnd     = iEnd;
+    }
+    return iPcreStatus;
+}
index b9bc293..618feb2 100644 (file)
 #ifndef __PCRE_PRIVATE_H__
 #define __PCRE_PRIVATE_H__
 
+#include <string.h>
+#include <wchar.h>
 #include "dynlib_string.h"
+
 typedef enum {
        PCRE_EXIT = 1,
        PCRE_FINISHED_OK = 0,
@@ -35,6 +38,7 @@ typedef enum {
 
 
 STRING_IMPEXP pcre_error_code pcre_private(char *INPUT_LINE,char *INPUT_PAT,int *Output_Start,int *Output_End);
+STRING_IMPEXP pcre_error_code wide_pcre_private(wchar_t* _pwstInput, wchar_t* _pwstPattern, int* _piStart, int* _piEnd);
 
 #endif /* __PCRE_PRIVATE_H__ */
 /*------------------------------------------------------------------------*/
index 6d31216..a5d8a6a 100644 (file)
                                >
                        </File>
                        <File
-                               RelativePath="..\..\sci_gateway\c\sci_strsubst.c"
-                               >
-                       </File>
-                       <File
                                RelativePath="..\..\sci_gateway\c\sci_strtod.c"
                                >
                        </File>
index dd014ff..2c182b3 100644 (file)
@@ -20,6 +20,7 @@
 #include "MALLOC.h"
 #include "pcre_private.h"
 #include "os_strdup.h"
+#include "os_wcsdup.h"
 #include "charEncoding.h"
 /*--------------------------------------------------------------------------*/
 char **strsubst(char **strings_input,int strings_dim,char *string_to_search,char *replacement_string)
@@ -231,3 +232,187 @@ char *strsub_reg(char* input_string, const char* string_to_search, const char* r
        return replacedString;
 }
 /*-------------------------------------------------------------------------------------*/
+wchar_t *wcssub_reg(wchar_t* _pwstInput, wchar_t* _pwstSearch, wchar_t* _pwstReplace, int* _piErr)
+{
+    int i;
+    pcre_error_code iPcreStatus = PCRE_FINISHED_OK;
+    int iStart                  = 0;
+    int iEnd                    = 0;
+    int iOccurs                 = 0;
+    int iStep                   = 0;
+    int iReplace                = 0;
+    int iOffset                 = 0;
+
+    int* piStart                = NULL;
+    int* piEnd                  = NULL;
+    
+    wchar_t* pwstOutput         = NULL;
+
+    if(_pwstInput == NULL)
+    {
+        return NULL;
+    }
+
+    if(_pwstSearch == NULL || _pwstReplace == NULL)
+    {
+        return os_wcsdup(_pwstInput);
+    }
+
+    iReplace    = (int)wcslen(_pwstReplace);
+
+    piStart     = (int*)MALLOC(sizeof(int) * wcslen(_pwstInput));
+    piEnd       = (int*)MALLOC(sizeof(int) * wcslen(_pwstInput));
+
+    do
+    {
+        iPcreStatus = wide_pcre_private(_pwstInput + iStep, _pwstSearch, &iStart, &iEnd);
+        if(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd)
+        {
+            //compute position on the output string, not original string
+            piStart[iOccurs]    = iStart + iStep;
+            piEnd[iOccurs++]    = iEnd + iStep;
+            iStep               += iEnd;
+            //compute new size of output string
+            iOffset             += iReplace - (iEnd - iStart);
+        }
+    }while(iPcreStatus == PCRE_FINISHED_OK && iStart != iEnd);
+
+    pwstOutput = (wchar_t*)MALLOC(sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
+    memset(pwstOutput, 0x00, sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
+
+    if(iOccurs == 0)
+    {
+        wcscpy(pwstOutput, _pwstInput);
+    }
+    else
+    {
+        for(i = 0 ; i < iOccurs ; i++)
+        {
+            if(i == 0)
+            {
+                //copy start of original string
+                wcsncpy(pwstOutput, _pwstInput, piStart[i]);
+            }
+            else
+            {
+                //copy start of original string
+                wcsncpy(pwstOutput + wcslen(pwstOutput), _pwstInput + piEnd[i - 1], piStart[i] - piEnd[i - 1]);
+            }
+            //copy replace string
+            wcscpy(pwstOutput + wcslen(pwstOutput), _pwstReplace);
+        }
+        //copy end of original string
+        wcscpy(pwstOutput + wcslen(pwstOutput), _pwstInput + piEnd[iOccurs - 1]);
+    }
+
+    FREE(piStart);
+    FREE(piEnd);
+
+    return pwstOutput;
+}
+/*-------------------------------------------------------------------------------------*/
+wchar_t **wcssubst_reg(wchar_t** _pwstInput, int _iInputSize, wchar_t* _pwstSearch, wchar_t* _pwstReplace, int* _piErr)
+{
+       wchar_t** pwstOutput = NULL;
+
+       if(_pwstInput != NULL && _pwstSearch != NULL && _pwstReplace != NULL)
+       {
+               int i = 0;
+               pwstOutput = (wchar_t**)MALLOC(sizeof(wchar_t*) * _iInputSize);
+               for(i = 0 ; i < _iInputSize ; i++)
+               {
+                       wchar_t* pwst = _pwstInput[i];
+                       pwstOutput[i] = wcssub_reg(pwst, _pwstSearch, _pwstReplace, _piErr);
+               }
+       }
+       return pwstOutput;
+}
+/*-------------------------------------------------------------------------------------*/
+wchar_t **wcssubst(wchar_t** _pwstInput, int _iInputSize, wchar_t* _pwstSearch, wchar_t* _pwstReplace)
+{
+       wchar_t** pwstOutput = NULL;
+
+       if(_pwstInput != NULL && _pwstSearch != NULL && _pwstReplace != NULL)
+       {
+               int i = 0;
+               pwstOutput = (wchar_t**)MALLOC(sizeof(wchar_t*) * _iInputSize);
+               for(i = 0 ; i < _iInputSize ; i++)
+               {
+                       wchar_t* pwst = _pwstInput[i];
+                       pwstOutput[i] = wcssub(pwst, _pwstSearch, _pwstReplace);
+               }
+       }
+       return pwstOutput;
+}
+/*-------------------------------------------------------------------------------------*/
+wchar_t *wcssub(wchar_t* _pwstInput, wchar_t* _pwstSearch, wchar_t* _pwstReplace)
+{
+    int i               = 0;
+    size_t iStep        = 0;
+    int iOccurs         = 0;
+    size_t iReplace     = 0;
+    size_t iSearch      = 0;
+    int iOffset         = 0;
+    
+    size_t* piStart     = NULL;
+
+    wchar_t* pwstPos    = NULL;
+    wchar_t* pwstOutput = NULL;
+
+    if(_pwstInput == NULL)
+    {
+        return NULL;
+    }
+
+    if(_pwstSearch == NULL || _pwstReplace == NULL)
+    {
+        return os_wcsdup(_pwstInput);
+    }
+
+    iSearch     = wcslen(_pwstSearch);
+    iReplace    = wcslen(_pwstReplace);
+    piStart     = (size_t*)MALLOC(sizeof(size_t) * wcslen(_pwstInput));
+    pwstPos     = _pwstInput;
+
+    while(pwstPos)
+    {
+        pwstPos = wcsstr(pwstPos, _pwstSearch);
+        if(pwstPos)
+        {
+            piStart[iOccurs++]  = pwstPos - _pwstInput;
+            iOffset             += iReplace - iSearch;
+            pwstPos++;
+        }
+    }
+
+    pwstOutput = (wchar_t*)MALLOC(sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
+    memset(pwstOutput, 0x00, sizeof(wchar_t) * (wcslen(_pwstInput) + iOffset + 1));
+
+    if(iOccurs == 0)
+    {
+        wcscpy(pwstOutput, _pwstInput);
+    }
+    else
+    {
+        for(i = 0 ; i < iOccurs ; i++)
+        {
+            if(i == 0)
+            {
+                //copy start of original string
+                wcsncpy(pwstOutput, _pwstInput, piStart[i]);
+            }
+            else
+            {
+                //copy start of original string
+                wcsncpy(pwstOutput + wcslen(pwstOutput), _pwstInput + piStart[i - 1] + iSearch, piStart[i] - (iSearch + piStart[i - 1]));
+            }
+            //copy replace string
+            wcscpy(pwstOutput + wcslen(pwstOutput), _pwstReplace);
+        }
+        //copy end of original string
+        wcscpy(pwstOutput + wcslen(pwstOutput), _pwstInput + piStart[iOccurs - 1] + iSearch);
+    }
+
+    return pwstOutput;
+}
+/*-------------------------------------------------------------------------------------*/