Markus Mottl avatar Markus Mottl committed cc341c6

Added names-function

Comments (0)

Files changed (6)

+2008-01-25:  Added new function:
+
+               * names
+
+             This function returns the names of all named substrings in a
+             regular expression.
+
+             Thanks to Benedikt Grundmann <bgrundmann@janestcapital.com>
+             for the patch!
+
 2007-07-12:  Improved build scripts for Windows.
 
              Thanks to Christophe Troestler
-release-5-12-2
+release-5-13-0
 name="pcre"
-version="5.11.4"
+version="5.13.0"
 description="Perl Compatibility Regular Expressions"
 requires=""
 archive(byte)="pcre.cma"
 (*
    PCRE-OCAML - Perl Compatibility Regular Expressions for OCaml
 
-   Copyright (C) 1999-2006  Markus Mottl
+   Copyright (C) 1999-  Markus Mottl
    email: markus.mottl@gmail.com
    WWW:   http://www.ocaml.info
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-(* $Id: pcre.ml,v 1.23 2006/01/17 00:38:30 mottl Exp $ *)
-
-
 (* Public exceptions and their registration with the C runtime *)
 
 exception Partial
 external capturecount : regexp -> int = "pcre_capturecount_stub"
 external backrefmax : regexp -> int = "pcre_backrefmax_stub"
 external namecount : regexp -> int = "pcre_namecount_stub"
+external names : regexp -> string array = "pcre_names_stub"
 external nameentrysize : regexp -> int = "pcre_nameentrysize_stub"
 external firstbyte : regexp -> firstbyte_info = "pcre_firstbyte_stub"
 external firsttable : regexp -> string option = "pcre_firsttable_stub"
 (*
    PCRE-OCAML - Perl Compatibility Regular Expressions for OCaml
 
-   Copyright (C) 1999-2006  Markus Mottl
+   Copyright (C) 1999-  Markus Mottl
    email: markus.mottl@gmail.com
    WWW:   http://www.ocaml.info
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)
 
-(* $Id: pcre.mli,v 1.21 2005/07/07 14:16:43 mottl Exp $ *)
-
 (** Perl Compatibility Regular Expressions *)
 
 
 (** [namecount regexp] @return number of named subpatterns in [regexp]. *)
 external namecount : regexp -> int = "pcre_namecount_stub"
 
+(** [names regex] @return array of names of named substrings in [regexp]. *)
+external names : regexp -> string array = "pcre_names_stub"
+
 (** [nameentrysize regexp] @return size of longest name of named
     subpatterns in [regexp] + 3. *)
 external nameentrysize : regexp -> int = "pcre_nameentrysize_stub"
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-/* $Id: pcre_stubs.c,v 1.23 2005/06/08 23:42:14 mottl Exp $ */
-
 #if defined(_WIN32) && defined(_DLL)
 #  define PCREextern __declspec(dllexport)
 #else
   return Val_int(ret);
 }
 
+/* Returns array of names of named substrings in a regexp */
+CAMLprim value pcre_names_stub(value v_rex)
+{
+  CAMLparam0();
+  CAMLlocal1(v_res);
+  int name_count;
+  int entry_size;
+  const char *tbl_ptr;
+  int i;
+
+  int ret = pcre_fullinfo_stub(v_rex, PCRE_INFO_NAMECOUNT, &name_count);
+  if (ret != 0) raise_with_string(*pcre_exc_InternalError, "pcre_names_stub");
+
+  ret = pcre_fullinfo_stub(v_rex, PCRE_INFO_NAMEENTRYSIZE, &entry_size);
+  if (ret != 0) raise_with_string(*pcre_exc_InternalError, "pcre_names_stub");
+
+  ret = pcre_fullinfo_stub(v_rex, PCRE_INFO_NAMETABLE, &tbl_ptr);
+  if (ret != 0) raise_with_string(*pcre_exc_InternalError, "pcre_names_stub");
+
+  v_res = caml_alloc(name_count, 0);
+
+  for (i = 0; i < name_count; ++i) {
+    value v_name = caml_copy_string(tbl_ptr + 2);
+    Store_field(v_res, i, v_name);
+    tbl_ptr += entry_size;
+  }
+
+  CAMLreturn(v_res);
+}
+
 /* Generic stub for getting integer results from pcre_config */
 static int pcre_config_int(int what)
 {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.