Text functions.
Funciones
Date Txt2Dte(Text txt, Text fmt)Real Txt2Month(Text txt)Set Txt2Set(Text txt, Set sep, Real cmp)Text TxtBetween2Tag(Text txt, Text tagIni, Text tagEnd, Real cmp)Text TxtInside2Tag(Text txt, Text tagIni, Text tagEnd)Text TxtOutside2Tag(Text txtInp, Text tagIni, Text tagEnd)Text TxtOutHtmTag(Text htmTxt)Text TxtOutHtmScr(Text htmTxt)Text TxtRand(Real len)Set TxtTokenizer(Text txtInp, Text tagBrk)//////////////////////////////////////////////////////////////////////////////
Date Txt2Dte(Text txt, // Text
Text fmt) // Format
//////////////////////////////////////////////////////////////////////////////
{
Case
(
fmt=="dd/mmm/yyyy",
{ // 123456789.1
Real day = Eval (Sub(txt, 1, 2)+"; ");
Real yea = Eval (Sub(txt, 8,11)+"; ");
Real mth = Txt2Month(Sub(txt, 4, 6));
If(Not(mth), UnknownDate, YMD(yea, mth, day))
},
TRUE, UnknownDate
)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a date from a text using some formats.
dd/mmm/yyyy | 07/Aug/2003 -> y2003m08d07
If the format is unknown returns the UnknownDate.",
Txt2Dte);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real Txt2Month(Text txt) // Text 3 or more letter
//////////////////////////////////////////////////////////////////////////////
{
Set engSet = SetTxtBeginWith(SetEnglishMonth, txt, FALSE);
If(GT(Card(engSet), 0), engSet[1],
{
Set spaSet = SetTxtBeginWith(SetSpanishMonth, txt, FALSE);
If(GT(Card(spaSet), 0), spaSet[1],
{
Set porSet = SetTxtBeginWith(SetPortugueseMonth, txt, FALSE);
If(GT(Card(porSet), 0), porSet[1],
{
Set freSet = SetTxtBeginWith(SetFrenchMonth, txt, FALSE);
If(GT(Card(freSet), 0), freSet[1], 0)
})
})
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a month number from the english, spanish, portuguese or french month
names (using this order).
If several months names match then returns the first one.
If none month match then returns 0.",
Txt2Month);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txt, // Text
Set sep, // Set of separators
Real cmp) // If true apply the Compact() function
//////////////////////////////////////////////////////////////////////////////
{
Text sepUni = Char(1); // Unique separator with only one character
Set sepTab = If(EQ(Card(sep),0), [[ [[";", sepUni ]] ]],
EvalSet(sep, Set(Text s) { [[s, sepUni]] }));
Text txtRep = ReplaceTable(txt, sepTab);
Set setTok = Tokenizer(txtRep, sepUni);
If(cmp, EvalSet(setTok, Text(Text txt) { Compact(txt) }), setTok)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set of texts like TOL function Tokenizer().
Can use a set of separators of any length.
Assumes that there are not Char(1) inside the text.
If argument sep is the Empty set then assumes ; as default separator.
If argument cmp is true then apply the Compact() function.
For example: Txt2Set(' a / b -- c / d / e / f ', [['/','--']], TRUE)
returns [['a','b','c','d','e','f']]",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text txt, // Text
Text tagIni, // Initial tag
Text tagEnd, // End tag
Real cmp) // If true apply the Compact() function
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result =
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
Text subTxt = If(LE(posEnd,0),
Sub(txt,posSub,TextLength(txt)),
Sub(txt,posSub,posEnd-1));
subTxt
});
If(cmp, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a subtext of text between the first ocurrence of tagIni and tagEnd.
If tagIni or tagEnd does not ocurr then returns the empty text.
If cmp argument is true then apply the Compact() function to the returning
text.
For example: TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
returns a 'c'.
There are other version of this function with more functionalities.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2Tag(Text txt, Text tagIni, Text tagEnd)
//
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
If(And(EQ(posIni,1),LE(posEnd,0)), txt,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txt,posIni, TextLength(txt)),
Sub(txt,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2Tag(Sub(txt, posEnd+TextLength(tagEnd), TextLength(txt)),
tagIni, tagEnd)))
});
ReplaceTable(result, [[ [[tagIni, ""]], [[tagEnd, ""]] ]])
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <::--->.",
TxtInside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutside2Tag(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Set txtSet = TxtTokenizer(tagIni + tagEnd + txtInp, tagIni);
Set txtCic = EvalSet(txtSet, Text(Text txtTok)
{ TxtBetween2Tag(txtTok + tagIni, tagEnd, tagIni, FALSE) });
SetSum(txtCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts outside 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(:::)ccc>, <(>, <)> -> <aaabbbccc>.",
TxtOutside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags and outside the scripts.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtRand(Real len) // Random text length
//////////////////////////////////////////////////////////////////////////////
{ SetSum(For(1,len,Text(Real t) { Char(Min(90,Max(65,Rand(64,91)))) })) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a random text with len chars from A to Z.
Use Min() y Max() because in some old versions of TOL the Rand() functions
sometimes returns numbers out of range.",
TxtRand);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set breaking the input text txtInp by the token tagBrk.
The lenght of the token tagBrk can be 1 or more characters. This function use
the Tol function Tokenizer() that breaks by only one character.
This function assume that txtInp do not contain the character 7 (bell).",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FILE : txt.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : Text functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Date Txt2Dte(Text txt, // Text
Text fmt) // Format
//////////////////////////////////////////////////////////////////////////////
{
Case
(
fmt=="dd/mmm/yyyy",
{ // 123456789.1
Real day = Eval (Sub(txt, 1, 2)+"; ");
Real yea = Eval (Sub(txt, 8,11)+"; ");
Real mth = Txt2Month(Sub(txt, 4, 6));
If(Not(mth), UnknownDate, YMD(yea, mth, day))
},
TRUE, UnknownDate
)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a date from a text using some formats.
dd/mmm/yyyy | 07/Aug/2003 -> y2003m08d07
If the format is unknown returns the UnknownDate.",
Txt2Dte);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real Txt2Month(Text txt) // Text 3 or more letter
//////////////////////////////////////////////////////////////////////////////
{
Set engSet = SetTxtBeginWith(SetEnglishMonth, txt, FALSE);
If(GT(Card(engSet), 0), engSet[1],
{
Set spaSet = SetTxtBeginWith(SetSpanishMonth, txt, FALSE);
If(GT(Card(spaSet), 0), spaSet[1],
{
Set porSet = SetTxtBeginWith(SetPortugueseMonth, txt, FALSE);
If(GT(Card(porSet), 0), porSet[1],
{
Set freSet = SetTxtBeginWith(SetFrenchMonth, txt, FALSE);
If(GT(Card(freSet), 0), freSet[1], 0)
})
})
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a month number from the english, spanish, portuguese or french month
names (using this order).
If several months names match then returns the first one.
If none month match then returns 0.",
Txt2Month);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txt, // Text
Set sep, // Set of separators
Real cmp) // If true apply the Compact() function
//////////////////////////////////////////////////////////////////////////////
{
Text sepUni = Char(1); // Unique separator with only one character
Set sepTab = If(EQ(Card(sep),0), [[ [[";", sepUni ]] ]],
EvalSet(sep, Set(Text s) { [[s, sepUni]] }));
Text txtRep = ReplaceTable(txt, sepTab);
Set setTok = Tokenizer(txtRep, sepUni);
If(cmp, EvalSet(setTok, Text(Text txt) { Compact(txt) }), setTok)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set of texts like TOL function Tokenizer().
Can use a set of separators of any length.
Assumes that there are not Char(1) inside the text.
If argument sep is the Empty set then assumes ; as default separator.
If argument cmp is true then apply the Compact() function.
For example: Txt2Set(' a / b -- c / d / e / f ', [['/','--']], TRUE)
returns [['a','b','c','d','e','f']]",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text txt, // Text
Text tagIni, // Initial tag
Text tagEnd, // End tag
Real cmp) // If true apply the Compact() function
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result =
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
Text subTxt = If(LE(posEnd,0),
Sub(txt,posSub,TextLength(txt)),
Sub(txt,posSub,posEnd-1));
subTxt
});
If(cmp, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a subtext of text between the first ocurrence of tagIni and tagEnd.
If tagIni or tagEnd does not ocurr then returns the empty text.
If cmp argument is true then apply the Compact() function to the returning
text.
For example: TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
returns a 'c'.
There are other version of this function with more functionalities.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2Tag(Text txt, Text tagIni, Text tagEnd)
//
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txt, tagEnd, posSub);
If(And(EQ(posIni,1),LE(posEnd,0)), txt,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txt,posIni, TextLength(txt)),
Sub(txt,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2Tag(Sub(txt, posEnd+TextLength(tagEnd), TextLength(txt)),
tagIni, tagEnd)))
});
ReplaceTable(result, [[ [[tagIni, ""]], [[tagEnd, ""]] ]])
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <::--->.",
TxtInside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutside2Tag(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Set txtSet = TxtTokenizer(tagIni + tagEnd + txtInp, tagIni);
Set txtCic = EvalSet(txtSet, Text(Text txtTok)
{ TxtBetween2Tag(txtTok + tagIni, tagEnd, tagIni, FALSE) });
SetSum(txtCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts outside 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(:::)ccc>, <(>, <)> -> <aaabbbccc>.",
TxtOutside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all text outside the Html tags and outside the scripts.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtRand(Real len) // Random text length
//////////////////////////////////////////////////////////////////////////////
{ SetSum(For(1,len,Text(Real t) { Char(Min(90,Max(65,Rand(64,91)))) })) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a random text with len chars from A to Z.
Use Min() y Max() because in some old versions of TOL the Rand() functions
sometimes returns numbers out of range.",
TxtRand);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a set breaking the input text txtInp by the token tagBrk.
The lenght of the token tagBrk can be 1 or more characters. This function use
the Tol function Tokenizer() that breaks by only one character.
This function assume that txtInp do not contain the character 7 (bell).",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
Ink.Watercolor construye las páginas del sitio web inkwatercolor.com
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio