From 6c447a486aee39a8fc78f15a23b270b7a231f612 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Wed, 5 Dec 2012 16:38:11 +0000 Subject: [PATCH] 2012-12-05 17:35 UTC+0100 Viktor Szakats (harbour syenar.net) * contrib/xhb/regexrpl.prg * contrib/xhb/xhb.hbx ! minor casing fix + contrib/xhb/tests/regexrpl.prg + added test from xhb (the function doesn't seem to work) + tests/regex.prg + tests/regextst.prg + added regex tests from xhb * contrib/hblzf/tests/test.prg ! typos * package/winuni/RELNOTES.txt * minor --- harbour/ChangeLog.txt | 19 +++++ harbour/contrib/hblzf/tests/test.prg | 18 ++--- harbour/contrib/xhb/regexrpl.prg | 2 +- harbour/contrib/xhb/tests/regexrpl.prg | 75 +++++++++++++++++ harbour/contrib/xhb/xhb.hbx | 2 +- harbour/package/winuni/RELNOTES.txt | 6 +- harbour/tests/regex.prg | 106 +++++++++++++++++++++++++ harbour/tests/regextst.prg | 63 +++++++++++++++ 8 files changed, 277 insertions(+), 14 deletions(-) create mode 100644 harbour/contrib/xhb/tests/regexrpl.prg create mode 100644 harbour/tests/regex.prg create mode 100644 harbour/tests/regextst.prg diff --git a/harbour/ChangeLog.txt b/harbour/ChangeLog.txt index e243aa58dd..54f30f93cb 100644 --- a/harbour/ChangeLog.txt +++ b/harbour/ChangeLog.txt @@ -10,6 +10,25 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2012-12-05 17:35 UTC+0100 Viktor Szakats (harbour syenar.net) + * contrib/xhb/regexrpl.prg + * contrib/xhb/xhb.hbx + ! minor casing fix + + + contrib/xhb/tests/regexrpl.prg + + added test from xhb (the function doesn't + seem to work) + + + tests/regex.prg + + tests/regextst.prg + + added regex tests from xhb + + * contrib/hblzf/tests/test.prg + ! typos + + * package/winuni/RELNOTES.txt + * minor + 2012-12-04 23:01 UTC+0100 Viktor Szakats (harbour syenar.net) * src/3rd/pcre/Makefile * src/3rd/pcre/pcre.dif diff --git a/harbour/contrib/hblzf/tests/test.prg b/harbour/contrib/hblzf/tests/test.prg index 8831890a32..e220163561 100644 --- a/harbour/contrib/hblzf/tests/test.prg +++ b/harbour/contrib/hblzf/tests/test.prg @@ -13,14 +13,14 @@ PROCEDURE Main() LOCAL b64_expected_result := "BFRoaXMgIAIUdGVzdCBvZiBMWkYgZXh0ZW5zaW9u" LOCAL nLen, nResult := 0 - ? "LZF Api version is", ; - hb_ntos( hb_lzf_version() ) + "(0x" + hb_NumToHex( hb_lzf_version() ) + ")" + ? "LZF API version is", ; + hb_ntos( hb_lzf_version() ) + " (0x" + hb_NumToHex( hb_lzf_version() ) + ")" ? "LibLZF optimized for", iif( hb_lzf_optimized_for_speed(), "speed.", "compression." ) ? "--- test 1 ---" /* If the output buffer is not large enough or any error occurs - hb_lzf_compress return NIL + hb_lzf_compress() returns NIL */ cStr := TEST_STRING str_compressed := hb_lzf_compress( cStr, 15, @nResult ) @@ -29,7 +29,7 @@ PROCEDURE Main() ? "Lenght of a string is", hb_ntos( Len( cStr ) ) ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) ELSE - ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ? "hb_lzf_compress() returns", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF ? "--- test 2 ---" @@ -41,7 +41,7 @@ PROCEDURE Main() ? "Lenght of a string is", hb_ntos( Len( cStr ) ) ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) ELSE - ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ? "hb_lzf_compress() returns", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF ? "--- test 3 ---" @@ -56,7 +56,7 @@ PROCEDURE Main() ? iif( hb_base64Encode( str_compressed ) == b64_expected_result, "OK!", "not OK!" ) ELSE - ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ? "hb_lzf_compress() returns", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF ? "--- test 4 ---" @@ -72,7 +72,7 @@ PROCEDURE Main() ? iif( hb_base64Encode( str_compressed ) == b64_expected_result, "OK!", "not OK!" ) ELSE - ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ? "hb_lzf_compress() returns", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF ? "--- test 5 ---" @@ -83,7 +83,7 @@ PROCEDURE Main() ? "Lenght of a string is", hb_ntos( Len( cStr ) ) ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) ELSE - ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ? "hb_lzf_compress() returns", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF ? "--- test 6 ---" @@ -128,7 +128,7 @@ PROCEDURE Main() str_decompressed := hb_lzf_decompress( str_compressed, @str_decompressed, @nResult ) IF nResult != HB_LZF_OK - ? "hb_lzf_decompress() return", ; + ? "hb_lzf_decompress() returns", ; iif( nResult == HB_LZF_MEM_ERROR, "HB_LZF_MEM_ERROR", hb_ntos( nResult ) ) ELSE ? iif( cStr == str_decompressed, "OK!", "not OK!" ) diff --git a/harbour/contrib/xhb/regexrpl.prg b/harbour/contrib/xhb/regexrpl.prg index b2474d142a..f3f578b6ee 100644 --- a/harbour/contrib/xhb/regexrpl.prg +++ b/harbour/contrib/xhb/regexrpl.prg @@ -53,7 +53,7 @@ #define MATCH_START 2 #define MATCH_END 3 -FUNCTION hb_RegexReplace( cRegex, cString, cReplace, lCaseSensitive, lNewLine, nMaxMatches, nGetMatch ) +FUNCTION hb_regexReplace( cRegex, cString, cReplace, lCaseSensitive, lNewLine, nMaxMatches, nGetMatch ) LOCAL aMatches, aMatch LOCAL cReturn diff --git a/harbour/contrib/xhb/tests/regexrpl.prg b/harbour/contrib/xhb/tests/regexrpl.prg new file mode 100644 index 0000000000..0b23dc6536 --- /dev/null +++ b/harbour/contrib/xhb/tests/regexrpl.prg @@ -0,0 +1,75 @@ +/* + * $Id$ + */ + +/* + * xHarbour Project test code: + * hb_regexReplace( cRegex, cString, cReplace, lCaseSensitive, lNewLine, nMaxMatches, nGetMatch ) --> cReturn + * + * Copyright 2006 Francesco Saverio Giudice + * www - http://www.xharbour.org + * + */ + +#require "xhb" + +PROCEDURE Main() + + LOCAL cString, cRegex, cReplace + + CLS + + ? "*** xHarbour hb_regexReplace() test ***" + ? + ? + ? "A simple replace, return is a single match, without submatches." + ? "Using 1 to retrieve matches" + cString := "aaabbbcccddd111222333aaabbbcccddd111222333" + cRegex := "aaa" + cReplace := "999" + ? "String: ", cString + ? "Result: ", hb_regexReplace( cRegEx, cString, cReplace,,,, 1 ) + ? + ? "A replace with a capturing match, return is a submatch." + ? "Using 2 to retrieve 1st submatches" + cString := "aaabbbcccddd111222333aaabbbcccddd111222333" + cRegex := "(aaa)" + cReplace := "999" + ? "String: ", cString + ? "Result: ", hb_regexReplace( cRegEx, cString, cReplace,,,, 2 ) + ? + WAIT + + ? + ? "Replacing a multiline string searching text that is on single line." + cString := "Hi folks! This is a real" + hb_eol() + "multiline text. Try this as a test." + cRegex := "(?im)this (.*) a" + cReplace := "" + ? "String: ", cString + ? "Result: ", hb_regexReplace( cRegEx, cString, cReplace,,,, 2 ) + ? + WAIT + + ? + ? "Replacing a multiline string searching text that is splitted on more lines." + cString := "Hi all. This is a" + hb_eol() + ; + "multiline text. Try this as a test" + hb_eol() + ; + "with another line of text." + cRegex := "(?ims)(.*?)" + cReplace := "" + ? "String: ", cString + ? "Result: ", hb_regexReplace( cRegEx, cString, cReplace,,,, 2 ) + ? + WAIT + + ? + ? "Replacing a multifield string." + cString := "/C=IT/O=xHarbour/OU=www.xharbour.com/CN=GIUDICE_FRANCESCO_SAVERIO/email=info@fsgiudice.com" + cRegex := "(?:\/(\w+)=)([\w.@]+)" + cReplace := "" + ? "String: ", cString + ? "Result: ", hb_regexReplace( cRegEx, cString, cReplace,,,, 2 ) + ? + WAIT + + RETURN diff --git a/harbour/contrib/xhb/xhb.hbx b/harbour/contrib/xhb/xhb.hbx index 41e34844c1..5190f1ac09 100644 --- a/harbour/contrib/xhb/xhb.hbx +++ b/harbour/contrib/xhb/xhb.hbx @@ -171,7 +171,7 @@ DYNAMIC hb_QSelf DYNAMIC hb_QWith DYNAMIC hb_ReadIni DYNAMIC hb_ReadLine -DYNAMIC hb_RegexReplace +DYNAMIC hb_regexReplace DYNAMIC hb_ResetWith DYNAMIC hb_ServiceGenerateFault DYNAMIC hb_ServiceGenerateFPE diff --git a/harbour/package/winuni/RELNOTES.txt b/harbour/package/winuni/RELNOTES.txt index 185b0117e4..f9735962d7 100644 --- a/harbour/package/winuni/RELNOTES.txt +++ b/harbour/package/winuni/RELNOTES.txt @@ -108,7 +108,7 @@ Changes since previous (2.0.0beta3 20090905) release: - New Harbour C level UTF-8 and UTF-16 string handling functions. - Rewritten internal codepage and collation support. Collations are now ensured to be CA-Cl*pper compatible. -- HB_TRANSLATE() now supports UTF-8. +- hb_Translate() now supports UTF-8. - OS/2 platform support improvements (watcom, .dll support, GCC OMF support). - gtxwc fixes. - General finalization to *nix Harbour dynamic library generation built into @@ -133,7 +133,7 @@ Changes since previous (2.0.0beta3 20090905) release: - Build system now uses automatic version detection for MSVC compilers. - Core trace message functionality improvements (new .prg level interface, Windows OutputDebugString support) -- New SET( _SET_DBCODEPAGE ). +- New Set( _SET_DBCODEPAGE ). - RDDADS fixes. - Removed support for harbour.cfg and Harbour compiler -go output mode. - New Harbour types (HB_SIZE, HB_BOOL, etc). @@ -191,7 +191,7 @@ Changes since previous (2.0.0beta1 20090609) release: - hbxbp and hbqt enhancements - hbwin refinements and better compatibility - new uhttpd2 sessioned HTTP server -- C++ build support for all compilers, new HB_WAEVAL() function, +- C++ build support for all compilers, new hb_WAEval() function, new xpp (Xbase++ compatibility) lib, .hrb loading enhancements - DBU, RL, SuperLib fixes. - MinGW64 updated to 4.4.1 diff --git a/harbour/tests/regex.prg b/harbour/tests/regex.prg new file mode 100644 index 0000000000..c8080d5044 --- /dev/null +++ b/harbour/tests/regex.prg @@ -0,0 +1,106 @@ +/* + * $Id$ + */ + +// Test for regular expression functions +// Giancarlo Niccolai + +PROCEDURE Main() + + LOCAL regex + LOCAL aMatch + LOCAL cStr, nRow := 2, nCol + LOCAL aSource := { ; + "First date to match: 2001-3-21", ; + "2002-12/2", ; + "Another can be 1999/5/12, and succeeds", ; + "Could be 1999/534/12, but this will fail" } + + CLS + + @ 0, 15 SAY "Regular expression scan tests" + /* + * Standard regex to get the ISO date format: + * ([0-9]{4}): exactly four digits (year); it is in brackets, + * this means that we want it back as a group + * [-/]: one bar or a minus + * ([0-9]{1,2}): one or two digits + */ + regex := hb_regexComp( "([0-9]{4})[-/]([0-9]{1,2})[-/]([0-9]{1,2})" ) + + FOR EACH cStr IN aSource + @ nRow, 5 SAY "String is '" + cStr + "'" + nRow++ + aMatch := hb_regex( regex, cStr ) + IF ! Empty( aMatch ) + @ nRow, 10 SAY "Matched: " + aMatch[ 1 ] + " ( Year: " + aMatch[ 2 ] + ", Month: " + ; + aMatch[ 3 ] + ", Day: " + aMatch[ 4 ] + ")" + ELSE + @ nRow, 10 SAY "Match FAILED!" + ENDIF + nRow += 2 + NEXT + + cStr := "searching 'regex' here:" + @ nRow, 5 SAY "A test of a regex compiled on the fly; " + cStr + aMatch := hb_regex( "(.*)regex(.*)", cStr ) + nRow++ + IF Empty( aMatch ) + @ nRow, 10 SAY "NOT FOUND!" + ELSE + @ nRow, 10 SAY "Found (Before: <<" + aMatch[ 2 ] + ">>, After: <<" + aMatch[ 3 ] + ">>)" + ENDIF + + nRow += 2 + + cStr := "A str; with: separators :; here " + @ nRow, 5 SAY "Split test; splitting '" + cStr + "' by ':|;'" + nRow++ + aMatch := hb_regexSplit( ":|;", cStr ) + IF Empty( aMatch ) + @ nRow++, 10 SAY "Test failed" + ELSE + nCol := 10 + FOR EACH cStr IN aMatch + @ nRow, nCol SAY cStr + "/" + nCol += Len( cStr ) + 1 + NEXT + nRow++ + ENDIF + + cStr := "A string without separators" + @ nRow, 5 SAY "Split test; splitting '" + cStr + "' by ':|;'" + nRow++ + aMatch := hb_regexSplit( ":|;", cStr ) + IF Empty( aMatch ) + @ nRow++, 10 SAY "Test failed" + ELSE + nCol := 10 + FOR EACH cStr IN aMatch + @ nRow, nCol SAY cStr + "/" + nCol += Len( cStr ) + 1 + NEXT + nRow++ + ENDIF + + cStr := "Test for RegexAtx" + @ nRow, 5 SAY "RegexAtx test; scanning '" + cStr + "' by 'Reg(.x)'" + nRow++ + aMatch := hb_regexAtX( "Reg(.x)", cStr ) + IF Empty( aMatch ) + @ nRow++, 10 SAY "Test failed" + ELSE + nCol := 15 + FOR EACH cStr in aMatch + @ nRow, nCol SAY "FOUND: '" + cStr[ 1 ] + "' Start: " + hb_ntos( cStr[ 2 ] ) + ; + " End: " + hb_ntos( cStr[ 3 ] ) + nRow++ + NEXT + nRow++ + ENDIF + + @ nRow, 1 + @ 24, 25 SAY "Press a key to continue" + Inkey( 0 ) + + RETURN diff --git a/harbour/tests/regextst.prg b/harbour/tests/regextst.prg new file mode 100644 index 0000000000..c3d057cc1a --- /dev/null +++ b/harbour/tests/regextst.prg @@ -0,0 +1,63 @@ +/* + * $Id$ + */ + +// Test for regular expression functions - +// This allows to use a fine tune regex to use them in programs +// Giancarlo Niccolai + +#include "inkey.ch" + +PROCEDURE Main() + + LOCAL pCompiled + LOCAL cRegex + LOCAL cSentence + LOCAL nRow, oErr + LOCAL aMatch, cMatch + + LOCAL GetList := {} + + SET CONFIRM ON + + CLS + + @ 2, 15 SAY "Regular expression test" + + @ 4, 5 SAY "Insert regular expression(s) and strings to test for." + @ 5, 5 SAY "Press to exit" + + cRegex := Space( 60 ) + cSentence := Space( 120 ) + DO WHILE LastKey() != K_ESC + + @ 8, 5 SAY "REGEX : " GET cRegex PICTURE "@S30" + @ 9, 5 SAY "PHRASE: " GET cSentence PICTURE "@S60" + READ + IF LastKey() != K_ESC + + @ 12, 5 CLEAR TO 24, 79 + + BEGIN SEQUENCE WITH {| oErr | Break( oErr ) } + pCompiled := hb_regexComp( RTrim( cRegex ) ) + RECOVER USING oErr + @ 12, 5 SAY "Invalid REGEX expression" + LOOP + END SEQUENCE + + aMatch := hb_regex( pCompiled, RTrim( cSentence ) ) + IF aMatch != NIL + @ 12, 5 SAY "MATCHES:" + nRow := 13 + FOR EACH cMatch IN aMatch + @ nRow++, 5 SAY ">" + cMatch + NEXT + ELSE + @ 12, 5 SAY "No matches" + ENDIF + ENDIF + ENDDO + + CLS + + RETURN