User:Ohconfucius/test/MOSNUM utils.js: Difference between revisions

Content deleted Content added
 
formatted, JSHint fixes
Line 10:
/// REGEX utility functions
 
function ohc_regex_to_string(s) {
str = s.toString();
{
str = sstr.toStringreplace(/^\//, "");
str = str.replace(/\/[^\/]*$/, "");
return str;
str = str.replace(/\/[^\/]*$/, "");
return str;
}
 
function ohc_alert_error(s, reg) { //reg can be undefined
var message = "DATES SCRIPT:\n" + s;
{
if (reg !== undefined) {
var message = "DATES SCRIPT:\n" + s;
message += "\n\nRegex" + reg;
if (reg !== undefined) {
}
message += "\n\nRegex" + reg;
message += "\n\nPlease report the name of the article to [????]";
}
alert(message);
message += "\n\nPlease report the name of the article to [????]";
alert(message);
}
 
/**
Main worker routine. The routine implements custom regex language to
simplify date transformations. The given regex and substitution strings
can contain magic words that specify what date format the routine should
accept and to what date format the routine should convert the date to.
Aside from magic strings, ordinary regular expressions and substitution
strings are accepted. Leading and trailing slashes are not needed for the
regex.
 
The magic string can be either capturing or non-capturing. The capturing
magic strings make their output available for later processing. Most of
the capturing magic strings can be used in the output with equivalent
meaning.
 
Internally, the routine maintains information about several dates, so the
regex can contain more than one magic string specifying day, month or year.
The first date will be assigned the first occurrences of magic strings from
each group, the second date will be assigned the second occurrences and so
on. For example, if the regex is '@MM @DD @YYYY, @Month @ZM @Day', the
first date will be assigned @MM as months, @DD as days and @YYYY as years.
The second date will be assigned @Month as months and @Day as days. The
third date will be assigned @ZM as months.
 
If used in the replacement string, all magic strings in the format @XX will
output the data of the first date. To access the subsequent dates, magic
strings in the format @XX2, @XX3, and so on must be used. @XX1 is provided
as alias to @XX for convenience.
 
AVAILABLE REGEX MAGIC STRINGS
=============================
 
Note: The capturing magic strings start with an uppercase letter whereas
the equivalent non-capturing magic strings start with a lowercase
letter.
 
Days:
@SD Matches a day in numeric format without leading zero (1..31).
 
@ZD Matches a day in numeric format with leading zero (01..31).
 
@DD Matches a day in numeric format with optional leading zero (@SD or @ZD)
 
@Day A day in numeric format with optional leading zero, with optional st,
nd or th suffix. Equivalent to @DD@th?
 
@sd, @zd, ... : noncapturing equivalents
 
@th Matches st, nd, rd or th
 
Months:
@SM A month in numeric format without leading zero (1,2..12).
 
@ZM A month in numeric format with leading zero (01,02..12).
 
@MM A month in numeric format with optional leading zero (@SM or @ZM)
 
@FullMonth Matches full name of a month (January, February)
 
@Mon Matches short name of a month (Jan, Feb, ..). Also optionally
matches dot (Jan., Feb., ..) and 'Sept', 'Sept.'.
 
@Month Matches full or short name of a month (@FullMonth or @Mon)
 
@sm, @zm, ... : noncapturing equivalents
 
Years:
@YYYY Matches a 4-digit year
 
@YY Matches a 2-digit year. 50-99 are interpreted as 1950..1999,
0-49 are interpreted as 2000-2049.
 
@YYNN Matches 4 or 2-digit year (@YYYY or @YY).
 
@Year Matches 1 to 4 digit year
 
@yyyy, @yy, ... : noncapturing equivalents
 
@@ Matches literal @
 
AVAILABLE REPLACEMENT STRING MAGIC STRINGS
==========================================
 
Days:
@SD: Outputs a day in numeric format without leading zero (1-31).
 
@ZD: Outputs a day in numeric format with leading zero (01-31).
 
@DD: Equivalent to @ZD
 
@Day: Equivalent to @SD
 
@LDay: Outputs the matched day string without any transformations
 
@SDn, @ZDn, ..., where n is integer
Outputs the day of the nth date in the specified format.
Months:
@SM Outputs a month in numeric format without leading zero (1-12)
 
@ZM Outputs a month in numeric format with leading zero (01-12)
 
@MM Equivalent to @ZM
 
@FullMonth Outputs a full name of a month (January, February)
 
@Mon Outputs a short name of a month (Jan, Feb)
 
@Month Equivalent to @FullMonth
 
@LMonth Outputs the matched month string without any transformations
 
@SMn, @ZMn, ..., where n is integer
Outputs the month of the nth date in the specified format.
 
Years:
@YYYY Outputs 4-digit year. Valid only if the year was not captured
by @Year
 
@YY Outputs 2-digit year. Outputs the last two digits of a year.
Valid only if the year was not captured by @Year
 
@YYNN Equivalent to @YYYY
 
@Year Outputs 1 to 4 digit number identifying a year. Equivalent to
@YYYY if the year is between 1000 and 9999
 
@LYear Outputs the matched year string without any transformations.
 
@YYYYn, @YYn, ..., where n is integer
Outputs the year of the nth date in the specified format.
 
@@ Outputs literal @
 
All magic strings in the replacement string, which refer to data which was
not defined, will be replaced witht '@ERROR@'
 
An optional function defining whether to make replacement in particular
case can be provided. The function is supplied a number of parameters, each
of which is an object defining the nth date as parsed by the routine. Each
object contains numeric values of days, months and years as 'd', 'm' and 'y'
properties respectively. Each value can be -1 if a magic string for that
date value was not specified in the regex, or an error occurs. The function
should return true if the replacement should be done, false otherwise.
*/
function ohc_regex(rg, sub, func) {
var reg = ohc_regex_to_string(rg);
{
var debug_reg = reg;
var reg = ohc_regex_to_string(rg);
var debug_reg = reg;
 
var month_names = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
"May", "June", "July", "August",
var month_names_short = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"September", "October", "November", "December");
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var month_names_short = new Array("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec");
 
var MAX_DATE = 4;
 
var ParamType = {
REGULAR : 1,
SD : 10,
ZD : 11,
DD : 12,
DAY : 13,
SM : 20,
ZM : 21,
MM : 22,
FMONTH : 23,
MONTH : 24,
MON : 25,
YYYY : 30,
YYNN : 31,
YY : 32,
YEAR : 33
};
 
var Group = {
SD : 10,
ZD DAY : 110,
DD MONTH : 121,
YEAR : 2
DAY : 13,
};
 
var Formats = [
SM : 20,
{ type : ParamType.SD, group : Group.DAY, magic : "@SD", match : /([1-9]|[1-2][0-9]|30|31)/ },
ZM : 21,
{ type : ParamType.ZD, group : Group.DAY, magic : "@ZD", match : /(0[1-9]|[1-2][0-9]|30|31)/ },
MM : 22,
{ type : ParamType.DD, group : Group.DAY, magic : "@DD", match : /(0?[1-9]|[1-2][0-9]|30|31)/ },
FMONTH : 23,
{ type : ParamType.DAY,group : Group.DAY, magic : "@Day",match : /((?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
MONTH : 24,
{ type : ParamType.SM, group : Group.MONTH,magic : "@SM", match : /([1-9]|10|11|12)/ },
MON : 25,
{ type : ParamType.ZM, group : Group.MONTH,magic : "@ZM", match : /(0[1-9]|10|11|12)/ },
{ type : ParamType.MM, group : Group.MONTH,magic : "@MM", match : /(0?[1-9]|10|11|12)/ },
{
type : ParamType.FMONTH, group : Group.MONTH, magic : "@FullMonth",
match : /(January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
type : ParamType.MONTH, group : Group.MONTH, magic : "@Month",
match : /(January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
type : ParamType.MON, group : Group.MONTH, magic : "@Mon",
match : /(Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ type : ParamType.YYYY, group : Group.YEAR, magic : "@YYYY", match : "([1-2][0-9]{3})" },
{ type : ParamType.YYNN, group : Group.YEAR, magic : "@YYNN", match : "([1-2][0-9]{3}|[0-9]{2})" },
{ type : ParamType.YY, group : Group.YEAR, magic : "@YY", match : "([0-9]{2})" }, //must be after yyyy and yy24 entries
{ type : ParamType.YEAR, group : Group.YEAR, magic : "@Year", match : "([1-2][0-9]{3}|[1-9][0-9]{0,2})" }
];
 
var NCFormats = [
YYYY : 30,
{ magic : "@sd", match : /(?:[1-9]|[1-2][0-9]|30|31)/ },
YYNN : 31,
{ magic : "@zd", match : /(?:0[1-9]|[1-2][0-9]|30|31)/ },
YY : 32,
{ magic : "@dd", match : /(?:0?[1-9]|[1-2][0-9]|30|31)/ },
YEAR : 33
{ magic : "@day",match : /(?:(?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
};
{ magic : "@sm", match : /(?:[1-9]|10|11|12)/ },
{ magic : "@zm", match : /(?:0[1-9]|10|11|12)/ },
{ magic : "@mm", match : /(?:0?[1-9]|10|11|12)/ },
{
magic : "@fullmonth",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
magic : "@month",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
magic : "@mon",
match : /(?:Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ magic : "@yyyy", match : /(?:[1-2][0-9]{3})/ },
{ magic : "@yynn", match : "(?:[1-2][0-9]{3}|[0-9]{2})" },
{ magic : "@yy", match : "(?:[0-9]{2})" }, //must be after yyyy and yy24 entries
{ magic : "@year", match : "(?:[1-2][0-9]{3}|[1-9][0-9]{0,2})" },
// misc
{ magic : "@th", match : "(?:th|st|nd|rd)" }
];
 
// get positions of all capturing matches in the regex
var Group = {
var params_by_index = {};
DAY : 0,
var capt_regex = /(?:^|[^\\])\((?!(?:\?:|\?=|\?!))/g;
MONTH : 1,
YEAR : 2
};
 
var Formats = [match;
var pi = 0;
{ type : ParamType.SD, group : Group.DAY, magic : "@SD", match : /([1-9]|[1-2][0-9]|30|31)/ },
while (match = capt_regex.exec(reg)) {
{ type : ParamType.ZD, group : Group.DAY, magic : "@ZD", match : /(0[1-9]|[1-2][0-9]|30|31)/ },
var param = {};
{ type : ParamType.DD, group : Group.DAY, magic : "@DD", match : /(0?[1-9]|[1-2][0-9]|30|31)/ },
param.index = match.index;
{ type : ParamType.DAY,group : Group.DAY, magic : "@Day",match : /((?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
param.type = ParamType.REGULAR;
{ type : ParamType.SM, group : Group.MONTH,magic : "@SM", match : /([1-9]|10|11|12)/ },
param.num = pi;
{ type : ParamType.ZM, group : Group.MONTH,magic : "@ZM", match : /(0[1-9]|10|11|12)/ },
params_by_index[match.index] = param;
{ type : ParamType.MM, group : Group.MONTH,magic : "@MM", match : /(0?[1-9]|10|11|12)/ },
pi++;
{
}
type : ParamType.FMONTH, group : Group.MONTH, magic : "@FullMonth",
match : /(January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
type : ParamType.MONTH, group : Group.MONTH, magic : "@Month",
match : /(January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
type : ParamType.MON, group : Group.MONTH, magic : "@Mon",
match : /(Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ type : ParamType.YYYY, group : Group.YEAR, magic : "@YYYY", match : "([1-2][0-9]{3})" },
{ type : ParamType.YYNN, group : Group.YEAR, magic : "@YYNN", match : "([1-2][0-9]{3}|[0-9]{2})" },
{ type : ParamType.YY, group : Group.YEAR, magic : "@YY", match : "([0-9]{2})" }, //must be after yyyy and yy24 entries
{ type : ParamType.YEAR, group : Group.YEAR, magic : "@Year", match : "([1-2][0-9]{3}|[1-9][0-9]{0,2})" }
];
 
// get positions of all capturing magic strings in the regex
var NCFormats = [
var magic_per_group = [0,0,0];
{ magic : "@sd", match : /(?:[1-9]|[1-2][0-9]|30|31)/ },
{ magic : "@zd", match : /(?:0[1-9]|[1-2][0-9]|30|31)/ },
{ magic : "@dd", match : /(?:0?[1-9]|[1-2][0-9]|30|31)/ },
{ magic : "@day",match : /(?:(?:[012]?[1-9]|10|20|30|31)(?:st|nd|rd|th|)?)/ },
{ magic : "@sm", match : /(?:[1-9]|10|11|12)/ },
{ magic : "@zm", match : /(?:0[1-9]|10|11|12)/ },
{ magic : "@mm", match : /(?:0?[1-9]|10|11|12)/ },
{
magic : "@fullmonth",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December)/
},
{
magic : "@month",
match : /(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ //must be after month entry
magic : "@mon",
match : /(?:Jan\.|Jan|Feb\.|Feb|Mar\.|Mar|Apr\.|Apr|May|Jun\.|Jun|Jul\.|Jul|Aug\.|Aug|Sep\.|Sept\.|Sept|Sep|Oct\.|Oct|Nov\.|Nov|Dec\.|Dec)/
},
{ magic : "@yyyy", match : /(?:[1-2][0-9]{3})/ },
{ magic : "@yynn", match : "(?:[1-2][0-9]{3}|[0-9]{2})" },
{ magic : "@yy", match : "(?:[0-9]{2})" }, //must be after yyyy and yy24 entries
{ magic : "@year", match : "(?:[1-2][0-9]{3}|[1-9][0-9]{0,2})" },
// misc
{ magic : "@th", match : "(?:th|st|nd|rd)" }
];
 
for (var i = 0; i < Formats.length; i++) {
// get positions of all capturing matches in the regex
var params_by_indexindex = {}-1;
while (1) {
var capt_regex = /(?:^|[^\\])\((?!(?:\?:|\?=|\?!))/g;
index = reg.indexOf(Formats[i].magic, index+1);
if (index == -1)
break;
if (params_by_index[index] === undefined) {
if (magic_per_group[index] > MAX_DATE) {
alert("DATE SCRIPT: unsupported number of dates from the same group");
return;
}
var param = {};
param.index = index;
param.type = Formats[i].type;
param.num = magic_per_group[Formats[i].group];
params_by_index[index] = param;
magic_per_group[Formats[i].group]++;
}
}
}
 
// pack the resulting array and sort by index
var match;
var piparam_desc = 0[];
for (var i in params_by_index) {
while (match = capt_regex.exec(reg)) {
param_desc.push(params_by_index[i]);
var param = {};
}
param.index = match.index;
param_desc.sort(function(a,b) {return a.index - b.index;});
param.type = ParamType.REGULAR;
param.num = pi;
params_by_index[match.index] = param;
pi++;
}
 
// get positions of all capturingreplace magic strings inwith theproper regexmatchs
for (var i = 0; i < Formats.length; i++) {
var magic_per_group = [0,0,0];
reg = reg.split(Formats[i].magic).join(ohc_regex_to_string(Formats[i].match));
}
for (var i = 0; i < NCFormats.length; i++) {
reg = reg.split(NCFormats[i].magic).join(ohc_regex_to_string(NCFormats[i].match));
}
reg = reg.split("@@").join("@");
 
//inline function for month parsing
for (var i = 0; i < Formats.length; i++) {
function parse_month(str) {
var index = -1;
var imonth = str.toLowerCase();
while (1) {
for (var i = 0; i < month_names_short.length; i++) {
index = reg.indexOf(Formats[i].magic, index+1);
if (imonth.substr(0,3) == month_names_short[i].toLowerCase()) {
if (index == -1) break;
return i+1;
if (params_by_index[index] === undefined) {
}
if (magic_per_group[index] > MAX_DATE) {
}
alert("DATE SCRIPT: unsupported number of dates from the same group");
return -1;
return;
}
}
var param = {};
param.index = index;
param.type = Formats[i].type;
param.num = magic_per_group[Formats[i].group];
params_by_index[index] = param;
magic_per_group[Formats[i].group]++;
}
};
}
 
//inline function for 2-digit year parsing
// pack the resulting array and sort by index
function parse_yy(str) {
var param_desc = [];
var y = parseInt(str, 10);
for (var i in params_by_index) {
if (y > 99) {
param_desc.push(params_by_index[i]);
return -1;
}
}
param_desc.sort(function(a,b) {return a.index - b.index;});
else if (y >= 20) {
return y + 1900;
}
else {
return y + 2000;
}
}
 
//inline function which will do the replacement
//replace magic strings with proper matchs
function regex_worker(str, m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10,
for (var i = 0; i < Formats.length; i++) {
m11, m12, m13, m14, m15, m16, m17, m18, m19) {
reg = reg.split(Formats[i].magic).join(ohc_regex_to_string(Formats[i].match));
var MAX_REGEX = 20;
}
for (var i = 0; i < NCFormats.length; i++) {
reg = reg.split(NCFormats[i].magic).join(ohc_regex_to_string(NCFormats[i].match));
}
reg = reg.split("@@").join("@");
 
// computed numeric values
//inline function for month parsing
var day = [];
function parse_month(str)
var month = [];
{
var imonthyear = str.toLowerCase()[];
for (var i = 0; i < month_names_short.length; i++) {
if (imonth.substr(0,3) == month_names_short[i].toLowerCase()) {
return i+1;
}
}
return -1;
}
 
// string values to output
//inline function for 2-digit year parsing
var raw_day = [];
function parse_yy(str)
var raw_month = [];
{
var raw_year = [];
var y = parseInt(str, 10);
var sday = [];
if (y > 99) {
var zday = [];
return -1;
var smonth = [];
} else if (y >= 20) {
var zmonth = [];
return y + 1900;
var full_month = [];
} else {
var short_month = [];
return y + 2000;
var year_yy = [];
}
var year_yyyy = [];
}
 
var regex_param = [];
//inline function which will do the replacement
function regex_worker(str, m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10,
m11, m12, m13, m14, m15, m16, m17, m18, m19)
{
var MAX_REGEX = 20;
 
//fill in computedthe numericinitial values
for (var i = 0; i < MAX_DATE; i++) {
var day = [];
raw_day[i] = raw_month[i] = raw_year[i] = "@ERROR@";
var month = [];
sday[i] = zday[i] = smonth[i] = zmonth[i] = full_month[i] = short_month[i] = "@ERROR@";
var year = [];
year_yy[i] = year_yyyy[i] = "@ERROR@";
day[i] = month[i] = year[i] = -1;
}
 
// save all arguments as an array
// string values to output
var raw_dayparams = [];
params.push(m0); params.push(m1); params.push(m2); params.push(m3);
var raw_month = [];
params.push(m4); params.push(m5); params.push(m6); params.push(m7);
var raw_year = [];
params.push(m8); params.push(m9); params.push(m10); params.push(m11);
var sday = [];
params.push(m12); params.push(m13); params.push(m14); params.push(m15);
var zday = [];
params.push(m16); params.push(m17); params.push(m18); params.push(m19);
var smonth = [];
var zmonth = [];
var full_month = [];
var short_month = [];
var year_yy = [];
var year_yyyy = [];
 
// parse the arguments according to the specification given as param_desc
var regex_param = [];
for (var i = 0; i < param_desc.length; i++) {
if (i > 19) {
alert("DATE SCRIPT: param id out of bounds");
return str;
}
 
var c_param = params[i]; //current param
//fill in the initial values
var c_desc = param_desc[i]; //current param description
for (var i = 0; i < MAX_DATE; i++) {
raw_day[i] = raw_month[i] = raw_year[i] = "@ERROR@";
sday[i] = zday[i] = smonth[i] = zmonth[i] = full_month[i] = short_month[i] = "@ERROR@";
year_yy[i] = year_yyyy[i] = "@ERROR@"
day[i] = month[i] = year[i] = -1;
}
 
switch (c_desc.type) {
// save all arguments as an array
case ParamType.REGULAR:
var params = [];
regex_param[c_desc.num] = c_param;
params.push(m0); params.push(m1); params.push(m2); params.push(m3);
break;
params.push(m4); params.push(m5); params.push(m6); params.push(m7);
params.push(m8); params.push(m9); params.push(m10); params.push(m11);
params.push(m12); params.push(m13); params.push(m14); params.push(m15);
params.push(m16); params.push(m17); params.push(m18); params.push(m19);
 
case ParamType.SD:
// parse the arguments according to the specification given as param_desc
case ParamType.ZD:
for (var i = 0; i < param_desc.length; i++) {
case ParamType.DD:
if (i > 19) {
day[c_desc.num] = parseInt(c_param, 10);
alert("DATE SCRIPT: param id out of bounds");
raw_day[c_desc.num] = c_param;
return str;
break;
}
 
case ParamType.DAY:
var c_param = params[i]; //current param
day[c_desc.num] = parseInt(c_param.replace(/[^0-9]/g, ''), 10);
var c_desc = param_desc[i]; //current param description
raw_day[c_desc.num] = c_param;
break;
 
case ParamType.SM:
switch (c_desc.type) {
case ParamType.REGULARZM:
case ParamType.MM:
regex_param[c_desc.num] = c_param;
month[c_desc.num] = parseInt(c_param, 10);
break;
raw_month[c_desc.num] = c_param;
break;
 
case ParamType.SDFMONTH:
case ParamType.ZDMONTH:
case ParamType.DDMON:
day month[c_desc.num] = parseIntparse_month(c_param, 10);
raw_day raw_month[c_desc.num] = c_param;
break;
 
case ParamType.DAYYY:
day year[c_desc.num] = parseIntparse_yy(c_param.replace(/[^0-9]/g, ''), 10);
raw_day raw_year[c_desc.num] = c_param;
break;
 
case ParamType.SMYEAR:
case ParamType.ZMYYYY:
year[c_desc.num] = parseInt(c_param, 10);
case ParamType.MM:
month raw_year[c_desc.num] = parseInt(c_param, 10);
break;
raw_month[c_desc.num] = c_param;
break;
 
case ParamType.FMONTHYYNN:
var yy = parse_yy(c_param, 10);
case ParamType.MONTH:
if (yy == -1) {
case ParamType.MON:
yy = parseInt(c_param, 10);
month[c_desc.num] = parse_month(c_param);
}
raw_month[c_desc.num] = c_param;
year[c_desc.num] = yy;
break;
raw_year[c_desc.num] = c_param;
break;
}
}
 
//catch errors, if any
case ParamType.YY:
for (var i = 0; i < MAX_DATE; i++) {
year[c_desc.num] = parse_yy(c_param);
if (day[i] == 0 || day[i] < -1 || day[i] > 31) {
raw_year[c_desc.num] = c_param;
ohc_alert_error("Invalid day [" + i + "]=" + day[i], debug_reg);
break;
day[i] = -1;
}
 
if (month[i] == 0 || month[i] < -1 || month[i] > 12) {
case ParamType.YEAR:
ohc_alert_error("Invalid month [" + i + "]=" + month[i], debug_reg);
case ParamType.YYYY:
month[i] = -1;
year[c_desc.num] = parseInt(c_param, 10);
}
raw_year[c_desc.num] = c_param;
break;
 
if (year[i] == 0 || year[i] < -1 || year[i] > 9999) {
case ParamType.YYNN:
ohc_alert_error("Invalid year [" + i + "]=" + year[i], debug_reg);
var yy = parse_yy(c_param, 10);
year[i] = -1;
if (yy == -1) {
}
yy = parseInt(c_param, 10);
}
}
year[c_desc.num] = yy;
raw_year[c_desc.num] = c_param;
break;
}
}
 
//check whether to make the replacement
//catch errors, if any
if (func !== undefined) {
for (var i = 0; i < MAX_DATE; i++) {
var d = [];
if (day[i] == 0 || day[i] < -1 || day[i] > 31) {
for (var i = 0; i < MAX_DATE; i++) {
ohc_alert_error("Invalid day [" + i + "]=" + day[i], debug_reg);
var di = {};
day[i] = -1;
di.d = day[i]; di.m = month[i]; di.y = year[i];
}
d.push(di);
}
if (func(d[0], d[1], d[2], d[3]) === false) {
return str;
}
}
 
//compute all needed formats
if (month[i] == 0 || month[i] < -1 || month[i] > 12) {
for (var i = 0; i < MAX_DATE; i++) {
ohc_alert_error("Invalid month [" + i + "]=" + month[i], debug_reg);
if month(day[i] != -1;) {
zday[i] = sday[i] = day[i].toString();
}
if (day[i] < 10) {
zday[i] = "0" + zday[i];
}
}
 
if (yearmonth[i] =!= 0 || year[i] < -1 || year[i] > 9999) {
zmonth[i] = smonth[i] = month[i].toString();
ohc_alert_error("Invalid year [" + i + "]=" + year[i], debug_reg);
if (month[i] < 10) {
year[i] = -1;
zmonth[i] = "0" + zmonth[i];
}
}
}
 
full_month[i] = month_names[month[i]-1];
//check whether to make the replacement
short_month[i] = month_names_short[month[i]-1];
if (func !== undefined) {
}
var d = [];
for (var i = 0; i < MAX_DATE; i++) {
var di = {};
di.d = day[i]; di.m = month[i]; di.y = year[i];
d.push(di);
}
if (func(d[0], d[1], d[2], d[3]) === false) {
return str;
}
}
 
if (year[i] != -1) {
//compute all needed formats
year_yyyy[i] = year[i].toString();
for (var i = 0; i < MAX_DATE; i++) {
if (year[i] >= 1950 && if (dayyear[i] !=< -12050) {
zday year_yy[i] = sdayyear_yyyy[i].charAt(2) =+ dayyear_yyyy[i].toStringcharAt(3);
}
if (day[i] < 10) {
}
zday[i] = "0" + zday[i];
}
}
}
 
//replace
if (month[i] != -1) {
var csub = sub;
zmonth[i] = smonth[i] = month[i].toString();
csub = csub.split("$1").join(regex_param[0]);
if (month[i] < 10) {
csub = csub.split("$2").join(regex_param[1]);
zmonth[i] = "0" + zmonth[i];
csub = csub.split("$3").join(regex_param[2]);
}
csub = csub.split("$4").join(regex_param[3]);
csub = csub.split("$5").join(regex_param[4]);
csub = csub.split("$6").join(regex_param[5]);
csub = csub.split("$7").join(regex_param[6]);
csub = csub.split("$8").join(regex_param[7]);
csub = csub.split("$9").join(regex_param[8]);
csub = csub.split("$10").join(regex_param[9]);
csub = csub.split("$11").join(regex_param[10]);
csub = csub.split("$12").join(regex_param[11]);
csub = csub.split("$13").join(regex_param[12]);
csub = csub.split("$14").join(regex_param[13]);
csub = csub.split("$15").join(regex_param[14]);
csub = csub.split("$16").join(regex_param[15]);
csub = csub.split("$17").join(regex_param[16]);
csub = csub.split("$18").join(regex_param[17]);
csub = csub.split("$19").join(regex_param[18]);
csub = csub.split("$20").join(regex_param[19]);
 
csub = csub.split("@SD4").join(sday[3]);
full_month[i] = month_names[month[i]-1];
csub = csub.split("@ZD4").join(zday[3]);
short_month[i] = month_names_short[month[i]-1];
csub = csub.split("@DD4").join(zday[3]);
}
csub = csub.split("@Day4").join(sday[3]);
csub = csub.split("@LDay4").join(raw_day[3]);
csub = csub.split("@SM4").join(smonth[3]);
csub = csub.split("@ZM4").join(zmonth[3]);
csub = csub.split("@MM4").join(zmonth[3]);
csub = csub.split("@FullMonth4").join(full_month[3]);
csub = csub.split("@Month4").join(full_month[3]);
csub = csub.split("@Mon4").join(short_month[3]);
csub = csub.split("@LMonth4").join(raw_month[3]);
csub = csub.split("@YYYY4").join(year_yyyy[3]);
csub = csub.split("@YYNN4").join(year_yyyy[3]);
csub = csub.split("@Year4").join(year_yyyy[3]);
csub = csub.split("@YY4").join(year_yy[3]);
csub = csub.split("@LYear4").join(raw_year[3]);
 
csub = csub.split("@SD3").join(sday[2]);
if (year[i] != -1) {
csub = csub.split("@ZD3").join(zday[2]);
year_yyyy[i] = year[i].toString();
csub = csub.split("@DD3").join(zday[2]);
if (year[i] >= 1950 && year[i] < 2050) {
csub = csub.split("@Day3").join(sday[2]);
year_yy[i] = year_yyyy[i].charAt(2) + year_yyyy[i].charAt(3);
csub = csub.split("@LDay3").join(raw_day[2]);
}
csub = csub.split("@SM3").join(smonth[2]);
}
csub = csub.split("@ZM3").join(zmonth[2]);
}
csub = csub.split("@MM3").join(zmonth[2]);
csub = csub.split("@FullMonth3").join(full_month[2]);
csub = csub.split("@Month3").join(full_month[2]);
csub = csub.split("@Mon3").join(short_month[2]);
csub = csub.split("@LMonth3").join(raw_month[2]);
csub = csub.split("@YYYY3").join(year_yyyy[2]);
csub = csub.split("@YYNN3").join(year_yyyy[2]);
csub = csub.split("@Year3").join(year_yyyy[2]);
csub = csub.split("@YY3").join(year_yy[2]);
csub = csub.split("@LYear3").join(raw_year[2]);
 
csub = csub.split("@SD2").join(sday[1]);
//replace
csub = csub.split("@ZD2").join(zday[1]);
var csub = sub;
csub = csub.split("$1@DD2").join(regex_paramzday[01]);
csub = csub.split("$2@Day2").join(regex_paramsday[1]);
csub = csub.split("$3@LDay2").join(regex_paramraw_day[21]);
csub = csub.split("$4@SM2").join(regex_paramsmonth[31]);
csub = csub.split("$5@ZM2").join(regex_paramzmonth[41]);
csub = csub.split("$6@MM2").join(regex_paramzmonth[51]);
csub = csub.split("$7@FullMonth2").join(regex_paramfull_month[61]);
csub = csub.split("$8@Month2").join(regex_paramfull_month[71]);
csub = csub.split("$9@Mon2").join(regex_paramshort_month[81]);
csub = csub.split("$10@LMonth2").join(regex_paramraw_month[91]);
csub = csub.split("$11@YYYY2").join(regex_paramyear_yyyy[101]);
csub = csub.split("$12@YYNN2").join(regex_paramyear_yyyy[111]);
csub = csub.split("$13@Year2").join(regex_paramyear_yyyy[121]);
csub = csub.split("$14@YY2").join(regex_paramyear_yy[131]);
csub = csub.split("$15@LYear2").join(regex_paramraw_year[141]);
csub = csub.split("$16").join(regex_param[15]);
csub = csub.split("$17").join(regex_param[16]);
csub = csub.split("$18").join(regex_param[17]);
csub = csub.split("$19").join(regex_param[18]);
csub = csub.split("$20").join(regex_param[19]);
 
csub = csub.split("@SD4SD1").join(sday[30]);
csub = csub.split("@ZD4ZD1").join(zday[30]);
csub = csub.split("@DD4DD1").join(zday[30]);
csub = csub.split("@Day4Day1").join(sday[30]);
csub = csub.split("@LDay4LDay1").join(raw_day[30]);
csub = csub.split("@SM4SM1").join(smonth[30]);
csub = csub.split("@ZM4ZM1").join(zmonth[30]);
csub = csub.split("@MM4MM1").join(zmonth[30]);
csub = csub.split("@FullMonth4FullMonth1").join(full_month[30]);
csub = csub.split("@Month4Month1").join(full_month[30]);
csub = csub.split("@Mon4Mon1").join(short_month[30]);
csub = csub.split("@LMonth4LMonth1").join(raw_month[30]);
csub = csub.split("@YYYY4YYYY1").join(year_yyyy[30]);
csub = csub.split("@YYNN4YYNN1").join(year_yyyy[30]);
csub = csub.split("@Year4Year1").join(year_yyyy[30]);
csub = csub.split("@YY4YY1").join(year_yy[30]);
csub = csub.split("@LYear4LYear1").join(raw_year[30]);
 
csub = csub.split("@SD3SD").join(sday[20]);
csub = csub.split("@ZD3ZD").join(zday[20]);
csub = csub.split("@DD3DD").join(zday[20]);
csub = csub.split("@Day3Day").join(sday[20]);
csub = csub.split("@LDay3LDay").join(raw_day[20]);
csub = csub.split("@SM3SM").join(smonth[20]);
csub = csub.split("@ZM3ZM").join(zmonth[20]);
csub = csub.split("@MM3MM").join(zmonth[20]);
csub = csub.split("@FullMonth3FullMonth").join(full_month[20]);
csub = csub.split("@Month3Month").join(full_month[20]); //this must be executed before the @Mon rule
csub = csub.split("@Mon3Mon").join(short_month[20]);
csub = csub.split("@LMonth3LMonth").join(raw_month[20]);
csub = csub.split("@YYYY3YYYY").join(year_yyyy[20]); //this must be executed before the @YY rule
csub = csub.split("@YYNN3YYNN").join(year_yyyy[20]);
csub = csub.split("@Year3Year").join(year_yyyy[20]);
csub = csub.split("@YY3YY").join(year_yy[20]);
csub = csub.split("@LYear3LYear").join(raw_year[20]);
 
csub = csub.split("@SD2@").join(sday[1]"@");
return csub;
csub = csub.split("@ZD2").join(zday[1]);
}
csub = csub.split("@DD2").join(zday[1]);
csub = csub.split("@Day2").join(sday[1]);
csub = csub.split("@LDay2").join(raw_day[1]);
csub = csub.split("@SM2").join(smonth[1]);
csub = csub.split("@ZM2").join(zmonth[1]);
csub = csub.split("@MM2").join(zmonth[1]);
csub = csub.split("@FullMonth2").join(full_month[1]);
csub = csub.split("@Month2").join(full_month[1]);
csub = csub.split("@Mon2").join(short_month[1]);
csub = csub.split("@LMonth2").join(raw_month[1]);
csub = csub.split("@YYYY2").join(year_yyyy[1]);
csub = csub.split("@YYNN2").join(year_yyyy[1]);
csub = csub.split("@Year2").join(year_yyyy[1]);
csub = csub.split("@YY2").join(year_yy[1]);
csub = csub.split("@LYear2").join(raw_year[1]);
 
//check whether a simple regex (i.e. without using regex_worker) would suffice
csub = csub.split("@SD1").join(sday[0]);
var simple_regex = true;
csub = csub.split("@ZD1").join(zday[0]);
for (var i = 0; i < param_desc.length; i++) {
csub = csub.split("@DD1").join(zday[0]);
if (param_desc[i].type != ParamType.REGULAR) {
csub = csub.split("@Day1").join(sday[0]);
simple_regex = false;
csub = csub.split("@LDay1").join(raw_day[0]);
break;
csub = csub.split("@SM1").join(smonth[0]);
}
csub = csub.split("@ZM1").join(zmonth[0]);
}
csub = csub.split("@MM1").join(zmonth[0]);
csub = csub.split("@FullMonth1").join(full_month[0]);
csub = csub.split("@Month1").join(full_month[0]);
csub = csub.split("@Mon1").join(short_month[0]);
csub = csub.split("@LMonth1").join(raw_month[0]);
csub = csub.split("@YYYY1").join(year_yyyy[0]);
csub = csub.split("@YYNN1").join(year_yyyy[0]);
csub = csub.split("@Year1").join(year_yyyy[0]);
csub = csub.split("@YY1").join(year_yy[0]);
csub = csub.split("@LYear1").join(raw_year[0]);
 
//do the replacement
csub = csub.split("@SD").join(sday[0]);
try {
csub = csub.split("@ZD").join(zday[0]);
var rg = new RegExp(reg,'gi');
csub = csub.split("@DD").join(zday[0]);
csub = csub.split("@Day").join(sday[0]);
csub = csub.split("@LDay").join(raw_day[0]);
csub = csub.split("@SM").join(smonth[0]);
csub = csub.split("@ZM").join(zmonth[0]);
csub = csub.split("@MM").join(zmonth[0]);
csub = csub.split("@FullMonth").join(full_month[0]);
csub = csub.split("@Month").join(full_month[0]); //this must be executed before the @Mon rule
csub = csub.split("@Mon").join(short_month[0]);
csub = csub.split("@LMonth").join(raw_month[0]);
csub = csub.split("@YYYY").join(year_yyyy[0]); //this must be executed before the @YY rule
csub = csub.split("@YYNN").join(year_yyyy[0]);
csub = csub.split("@Year").join(year_yyyy[0]);
csub = csub.split("@YY").join(year_yy[0]);
csub = csub.split("@LYear").join(raw_year[0]);
 
var editbox = $('#wpTextbox1'), text = editbox.val();
csub = csub.split("@@").join("@");
if (simple_regex == true)
return csub;
text = text.replace(rg, sub);
}
else
text = text.replace(rg, regex_worker);
editbox.val(text);
 
var aa_reg = debug_reg; //place for a breakpoint for debugging
//check whether a simple regex (i.e. without using regex_worker) would suffice
}
var simple_regex = true;
catch(err) {
for (var i = 0; i < param_desc.length; i++) {
var message = "Error in regex execution\n" + "ERROR: " + err.message + "\n";
if (param_desc[i].type != ParamType.REGULAR) {
ohc_alert_error(message, debug_reg);
simple_regex = false;
}
break;
}
}
 
//do the replacement
try {
var rg = new RegExp(reg,'gi');
 
var editbox = $('#wpTextbox1'), text = editbox.val();
if (simple_regex == true)
text = text.replace(rg, sub);
else
text = text.replace(rg, regex_worker);
editbox.val(text);
 
var aa_reg = debug_reg; //place for a breakpoint for debugging
} catch(err) {
var message = "Error in regex execution\n" + "ERROR: " + err.message + "\n";
ohc_alert_error(message, debug_reg);
}
}