Skip to content

Commit

Permalink
Add function Strings.contains (#4493)
Browse files Browse the repository at this point in the history
* Add function Strings.contains

* Return true for empty search strings of Strings.contains

In accordance with python, java, etc.

* Add tests for Strings.contains

* Fix documentation

* Fix documentation

Co-authored-by: tobolar <tobolar@users.noreply.github.com>

* Update documentation

Co-authored-by: tobolar <tobolar@users.noreply.github.com>

---------

Co-authored-by: mkr7 <marco.kessler@3ds.com>
Co-authored-by: tobolar <tobolar@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent 1166ac5 commit 251d7f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Modelica/Utilities/Strings.mo
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,52 @@ isEmpty(\"a\"); // returns false
</html>"));
end isEmpty;

function contains "Check if a string contains a substring"

extends Modelica.Icons.Function;

input String string "String to check";
input String searchString "Substring to look for";
input Boolean caseSensitive=true
"= false, if lower and upper case are ignored for the search";
output Boolean result "= true, if searchString can be found anywhere in string";

algorithm
if length(searchString) > 0 then
result := find(string, searchString, 1, caseSensitive) > 0;
else
result := true;
end if;

annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
result = Strings.<strong>contains</strong>(string, searchString);
result = Strings.<strong>contains</strong>(string, searchString, caseSensitive);
</pre></blockquote>
<h4>Description</h4>
<p>
Returns true if \"searchString\" is a substring of \"string\". Otherwise, false is returned.
The optional argument \"caseSensitive\" controls if substrings match, which differ in case only.
</p>
<p>
Empty strings are treated as substring of all strings. Therefore, the result is always true if \"searchString\" is empty.
</p>
<h4>Example</h4>
<blockquote><pre>
import Modelica.Utilities.Strings.contains;
contains(\"foobar\", \"OO\"); // returns false
contains(\"foobar\", \"OO\", false); // returns true
contains(\"foo\", \"bar\"); // returns false
contains(\"\", \"\"); // returns true
contains(\"foo\", \"\"); // returns true
</pre></blockquote>
</html>"));
end contains;

function count "Count the number of non-overlapping occurrences of a string"
extends Modelica.Icons.Function;
input String string "String that is analyzed";
Expand Down
11 changes: 11 additions & 0 deletions ModelicaTest/Utilities.mo
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ extends Modelica.Icons.ExamplesPackage;
assert(hash1 <> Strings.hashString("this is a tes1"), "Strings.hashString 1 failed");
assert(hash1 <> Strings.hashString("this is a tes2"), "Strings.hashString 1 failed");

// Strings.contains
assert(Strings.contains("", ""), "Strings.contains 1 failed (empty strings)");
assert(Strings.contains("foo bar", "o"), "Strings.contains 2 failed (multiple occurences)");
assert(Strings.contains("foo bar", "o ba"), "Strings.contains 3 failed (occurrence at middle)");
assert(Strings.contains("foo bar", "fo"), "Strings.contains 4 failed (occurrence at start)");
assert(Strings.contains("foo bar", "ar"), "Strings.contains 5 failed (occurrence at end)");
assert(not Strings.contains("foo bar", "x"), "Strings.contains 6 failed (no occurrence)");
assert(Strings.contains("foo", ""), "Strings.contains 7 failed (empty search string)");
assert(not Strings.contains("foo bar", "O"), "Strings.contains 8 failed (case sensitive)");
assert(Strings.contains("foo bar", "O", false), "Strings.contains 9 failed (case insensitive)");

ok := true;
end Strings;

Expand Down

0 comments on commit 251d7f4

Please sign in to comment.