Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP and Java Runners #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added runners/Java/Evaluate.class
Binary file not shown.
23 changes: 23 additions & 0 deletions runners/Java/JavaRunner/src/runner/Evaluate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package runner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Evaluate {
public Evaluate(String pattern, String corpus_text){
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(corpus_text);

String json = "{";
int c = 0, l;
while(m.find()){
c++;
l = m.end() - m.start();
json += "\"" + l + "\":[" + m.start() + "," + l + "],";
}
json += "\"matchSummary\":{\"total\":" + c + "}";
json += "}";

System.out.println(json);
}
}
11 changes: 11 additions & 0 deletions runners/Java/JavaRunner/src/runner/Replace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runner;

import java.util.regex.Pattern;

public class Replace {
public Replace(String pattern, String corpus_text, String replace){
// Nice and short :)
String json = "{\"replace\":\"" + Pattern.compile(pattern).matcher(corpus_text).replaceAll(replace) + "\"}";
System.out.println(json);
}
}
26 changes: 26 additions & 0 deletions runners/Java/JavaRunner/src/runner/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package runner;
/**
* Java regex runner for Refiddle
* Written by FelisPhasma
* Copyright (c) FelisPhasma 2015 Under the MIT License
*
*/

public class Runner {
public static void main(String args[]){
if(args.length == 0)
return;
String action = args[0];
String pattern = args[1];
String corpus_text = args[2];
String replace = args.length >=4 ? args[3] : "";

if(action == "evaluate"){
new Evaluate(pattern, corpus_text);
} else if(action == "replace"){
new Replace(pattern, corpus_text, replace);
} else {
System.out.println("{\"error\":\"Internal server error: unclear action\"}");
}
}
}
Binary file added runners/Java/Replace.class
Binary file not shown.
Binary file added runners/Java/Runner.class
Binary file not shown.
27 changes: 27 additions & 0 deletions runners/Java/evaluate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* PHP messenger for the Java regex runner for Refiddle
* This code will handle form posts from Regiddle intended
* for the Java regex runner. Mainly because I do not want
* to write it entirely in JSP.
*
* Written by FelisPhasma
* Copyright (c) FelisPhasma 2015 Under the MIT License
*
*/

if(empty($_POST)){
// Safeguard against empty requests
echo '{
"matchSummary": {
"total":0
}
}';
} else {
// Get reguests values
$pattern = $_POST["pattern"];
$corpus_text = $_POST["corpus_text"];

$output = shell_exec('/Runner.class "evaluate" ' . escapeshellarg($pattern) . ' ' . escapeshellarg($corpus_text));
echo $output;
}
18 changes: 18 additions & 0 deletions runners/Java/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The MIT License (MIT)

Copyright (c) 2015 FelisPhasma

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions runners/Java/regexOptions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
+-------------------------+------------------------------------------------------------------------------------------+
| Flag | Modification |
+=========================+==========================================================================================+
| CASE_INSENSITIVE | Enables case-insensitive matching. |
| MULTILINE | Enables multiline mode. |
| DOTALL | Enables dotall mode. |
| UNICODE_CASE | Enables Unicode-aware case folding. |
| CANON_EQ | Enables canonical equivalence. |
| UNIX_LINES | Enables Unix lines mode. |
| LITERAL | Enables literal parsing of the pattern. |
| UNICODE_CHARACTER_CLASS | Enables the Unicode version of Predefined character classes and POSIX character classes. |
| COMMENTS | Permits whitespace and comments in pattern. |
+-------------------------+------------------------------------------------------------------------------------------+
| Source: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html |
+--------------------------------------------------------------------------------------------------------------------+
26 changes: 26 additions & 0 deletions runners/Java/replace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* PHP messenger for the Java regex runner for Refiddle
* This code will handle form posts from Regiddle intended
* for the Java regex runner. Mainly because I do not want
* to write it entirely in JSP.
*
* Written by FelisPhasma
* Copyright (c) FelisPhasma 2015 Under the MIT License
*
*/

if(empty($_POST)){
// Safeguard against empty requests
echo '{
"replace": ""
}';
} else {
// Get reguests values
$pattern = $_POST["pattern"];
$corpus_text = $_POST["corpus_text"];
$replace_text = $_POST["replace_text"];

$output = shell_exec('/Runner.class "replace" ' . escapeshellarg($pattern) . ' ' . escapeshellarg($corpus_text) . ' ' . escapeshellarg($replace_text));
echo $output;
}
46 changes: 46 additions & 0 deletions runners/PHP/evaluate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* PHP regex runner for Refiddle
* Written by FelisPhasma
* Copyright (c) FelisPhasma 2015 Under the MIT License
*
*/

// Custom error handeling is used, so turn off the error reporting
error_reporting(0);
@ini_set('display_errors', 0);

if(empty($_POST)){
// Safeguard against empty requests
echo '{
"matchSummary": {
"total":0
}
}';
} else {
// Get reguests values
$pattern = $_POST["pattern"];
$corpus_text = $_POST["corpus_text"];

// Provide error information for the user
set_error_handler(function($errno, $errstr, $errfile, $errline) {
$json = "{";
$json .= '"error":"' . substr($errstr, 18) . '"';
$json .= "}";
echo $json;
die();
});

// Evaluation
preg_match_all($pattern, $corpus_text, $matches, PREG_OFFSET_CAPTURE);
$json = "{";
$realMatches = $matches[0];
foreach($realMatches as &$item){
$json .= '"' . $item[1] . '": [' . $item[1] . ',' . strlen($item[0]) . '],';
}
$json .= '"matchSummary": {
"total":' . count($matches) . '
}';
$json .= "}";
echo $json;
}
18 changes: 18 additions & 0 deletions runners/PHP/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The MIT License (MIT)

Copyright (c) 2015 FelisPhasma

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 changes: 14 additions & 0 deletions runners/PHP/regexOptions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
+------+--------------------------------------------------------------------+
| Flag | Modification |
+======+====================================================================+
| i | Caseless – ignore case |
| m | Multiline mode - ^ and $ match start and end of lines |
| s | Dotall - . class includes newline |
| x | Extended– comments & whitespace |
| e | preg_replace only – enables evaluation of replacement as PHP code |
| S | Extra analysis of pattern |
| U | Pattern is ungreedy |
| u | Pattern is treated as UTF-8 |
+------+--------------------------------------------------------------------+---------------------------+
| Source: https://courses.cs.washington.edu/courses/cse190m/12sp/cheat-sheets/php-regex-cheat-sheet.pdf |
+-------------------------------------------------------------------------------------------------------+
39 changes: 39 additions & 0 deletions runners/PHP/replace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* PHP regex runner for Refiddle
* Written by FelisPhasma
* Copyright (c) FelisPhasma 2015 Under the MIT License
*
*/

// Custom error handeling is used, so turn off the error reporting
error_reporting(0);
@ini_set('display_errors', 0);

if(empty($_POST)){
// Safeguard against empty requests
echo '{
"replace": ""
}';
} else {
// Get reguests values
$pattern = $_POST["pattern"];
$corpus_text = $_POST["corpus_text"];
$replace_text = $_POST["replace_text"];

// Provide error information for the user
set_error_handler(function($errno, $errstr, $errfile, $errline) {
$json = "{";
$json .= '"error":"' . $errstr . '"';
$json .= "}";
echo $json;
die();
});

// Corpus text replacements
$replaced = preg_replace($pattern, $replace_text, $corpus_text);
$json = "{";
$json .= '"replace":' . $replaced;
$json .= "}";
echo $json;
}
6 changes: 6 additions & 0 deletions runners/Python/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cgi
form = cgi.FieldStorage()

pattern = form["pattern"]
corpus_text = form["corpus_text"]

7 changes: 7 additions & 0 deletions runners/Python/replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import cgi
form = cgi.FieldStorage()

pattern = form["pattern"]
corpus_text = form["corpus_text"]
replace_text = form["replace_text"]