|
15 | 15 |
|
16 | 16 | from mathics.core.atoms import Integer, Integer0, Integer1, String
|
17 | 17 | from mathics.core.attributes import A_LISTABLE, A_PROTECTED
|
18 |
| -from mathics.core.builtin import Builtin, Predefined, PrefixOperator, Test |
| 18 | +from mathics.core.builtin import Builtin, Predefined, PrefixOperator |
19 | 19 | from mathics.core.convert.expression import to_mathics_list
|
20 |
| -from mathics.core.convert.python import from_bool |
21 |
| -from mathics.core.convert.regex import to_regex |
22 | 20 | from mathics.core.evaluation import Evaluation
|
23 | 21 | from mathics.core.expression import Expression
|
24 | 22 | from mathics.core.list import ListExpression
|
25 | 23 | from mathics.core.parser import MathicsFileLineFeeder, parse
|
26 |
| -from mathics.core.symbols import Symbol, SymbolTrue |
27 | 24 | from mathics.core.systemsymbols import (
|
28 | 25 | SymbolFailed,
|
29 | 26 | SymbolInputForm,
|
30 | 27 | SymbolNone,
|
31 | 28 | SymbolOutputForm,
|
| 29 | + SymbolToExpression, |
32 | 30 | )
|
33 |
| -from mathics.eval.strings import eval_ToString |
| 31 | +from mathics.eval.strings import eval_StringContainsQ, eval_ToString |
34 | 32 | from mathics.settings import SYSTEM_CHARACTER_ENCODING
|
35 | 33 |
|
36 |
| -SymbolToExpression = Symbol("ToExpression") |
37 |
| - |
38 | 34 | # covers all of the variations. Here we just give some minimal basics
|
39 | 35 |
|
40 | 36 | # Data taken from:
|
@@ -130,48 +126,6 @@ def push(i, iter, form):
|
130 | 126 | push(i, iter, form)
|
131 | 127 |
|
132 | 128 |
|
133 |
| -def _pattern_search(name, string, patt, evaluation, options, matched): |
134 |
| - # Get the pattern list and check validity for each |
135 |
| - if patt.has_form("List", None): |
136 |
| - patts = patt.elements |
137 |
| - else: |
138 |
| - patts = [patt] |
139 |
| - re_patts = [] |
140 |
| - for p in patts: |
141 |
| - py_p = to_regex(p, show_message=evaluation.message) |
142 |
| - if py_p is None: |
143 |
| - evaluation.message("StringExpression", "invld", p, patt) |
144 |
| - return |
145 |
| - re_patts.append(py_p) |
146 |
| - |
147 |
| - flags = re.MULTILINE |
148 |
| - if options["System`IgnoreCase"] is SymbolTrue: |
149 |
| - flags = flags | re.IGNORECASE |
150 |
| - |
151 |
| - def _search(patts, str, flags, matched): |
152 |
| - if any(re.search(p, str, flags=flags) for p in patts): |
153 |
| - return from_bool(matched) |
154 |
| - return from_bool(not matched) |
155 |
| - |
156 |
| - # Check string validity and perform regex searchhing |
157 |
| - if string.has_form("List", None): |
158 |
| - py_s = [s.get_string_value() for s in string.elements] |
159 |
| - if any(s is None for s in py_s): |
160 |
| - evaluation.message( |
161 |
| - name, "strse", Integer1, Expression(Symbol(name), string, patt) |
162 |
| - ) |
163 |
| - return |
164 |
| - return to_mathics_list(*[_search(re_patts, s, flags, matched) for s in py_s]) |
165 |
| - else: |
166 |
| - py_s = string.get_string_value() |
167 |
| - if py_s is None: |
168 |
| - evaluation.message( |
169 |
| - name, "strse", Integer1, Expression(Symbol(name), string, patt) |
170 |
| - ) |
171 |
| - return |
172 |
| - return _search(re_patts, py_s, flags, matched) |
173 |
| - |
174 |
| - |
175 | 129 | def anchor_pattern(patt):
|
176 | 130 | """
|
177 | 131 | anchors a regex in order to force matching against an entire string.
|
@@ -691,35 +645,11 @@ class StringContainsQ(Builtin):
|
691 | 645 |
|
692 | 646 | def eval(self, string, patt, evaluation: Evaluation, options: dict):
|
693 | 647 | "StringContainsQ[string_, patt_, OptionsPattern[%(name)s]]"
|
694 |
| - return _pattern_search( |
| 648 | + return eval_StringContainsQ( |
695 | 649 | self.__class__.__name__, string, patt, evaluation, options, True
|
696 | 650 | )
|
697 | 651 |
|
698 | 652 |
|
699 |
| -class StringQ(Test): |
700 |
| - """ |
701 |
| - <url> |
702 |
| - :WMA link: |
703 |
| - https://reference.wolfram.com/language/ref/StringQ.html</url> |
704 |
| - <dl> |
705 |
| - <dt>'StringQ[$expr$]' |
706 |
| - <dd>returns 'True' if $expr$ is a 'String', or 'False' otherwise. |
707 |
| - </dl> |
708 |
| -
|
709 |
| - >> StringQ["abc"] |
710 |
| - = True |
711 |
| - >> StringQ[1.5] |
712 |
| - = False |
713 |
| - >> Select[{"12", 1, 3, 5, "yz", x, y}, StringQ] |
714 |
| - = {12, yz} |
715 |
| - """ |
716 |
| - |
717 |
| - summary_text = "test whether an expression is a string" |
718 |
| - |
719 |
| - def test(self, expr) -> bool: |
720 |
| - return isinstance(expr, String) |
721 |
| - |
722 |
| - |
723 | 653 | class StringRepeat(Builtin):
|
724 | 654 | """
|
725 | 655 | <url>
|
|
0 commit comments