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

Add function Strings.contains #4493

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions Modelica/Utilities/Strings.mo
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,54 @@ 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>
beutlich marked this conversation as resolved.
Show resolved Hide resolved
<blockquote><pre>
Strings.<strong>contains</strong>(string, searchString);
Strings.<strong>contains</strong>(string, searchString, caseSensitive=true);
beutlich marked this conversation as resolved.
Show resolved Hide resolved
</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.
beutlich marked this conversation as resolved.
Show resolved Hide resolved
</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(\"foo\", \"\"); // returns false
contains(\"\", \"\"); // returns true
contains(\"foo\", \"\"); // returns true
beutlich marked this conversation as resolved.
Show resolved Hide resolved
</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