Skip to content

Commit

Permalink
fix issue #173 convertStringsToChars builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Jan 3, 2019
1 parent d2e27f2 commit 8725ab7
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@
Features:
---------

* [#173](http://github.com/Nelson-numerical-software/nelson/issues/173): convertStringsToChars builtin.



Bug Fixes:
Expand Down
2 changes: 2 additions & 0 deletions modules/string/builtin/c/nlsString_builtin.vcxproj
Expand Up @@ -170,6 +170,7 @@
<ItemGroup>
<ClCompile Include="..\cpp\charBuiltin.cpp" />
<ClCompile Include="..\cpp\containsBuiltin.cpp" />
<ClCompile Include="..\cpp\convertStringsToCharsBuiltin.cpp" />
<ClCompile Include="..\cpp\countBuiltin.cpp" />
<ClCompile Include="..\cpp\deblankBuiltin.cpp" />
<ClCompile Include="..\cpp\endsWithBuiltin.cpp" />
Expand Down Expand Up @@ -224,6 +225,7 @@
<ItemGroup>
<ClInclude Include="..\include\charBuiltin.hpp" />
<ClInclude Include="..\include\containsBuiltin.hpp" />
<ClInclude Include="..\include\convertStringsToCharsBuiltin.hpp" />
<ClInclude Include="..\include\countBuiltin.hpp" />
<ClInclude Include="..\include\deblankBuiltin.hpp" />
<ClInclude Include="..\include\endsWithBuiltin.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions modules/string/builtin/c/nlsString_builtin.vcxproj.filters
Expand Up @@ -84,6 +84,9 @@
<ClCompile Include="..\cpp\deblankBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\convertStringsToCharsBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down Expand Up @@ -155,6 +158,9 @@
<ClInclude Include="..\include\deblankBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\convertStringsToCharsBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\loader.nls" />
Expand Down
6 changes: 5 additions & 1 deletion modules/string/builtin/cpp/Gateway.cpp
Expand Up @@ -38,6 +38,7 @@
#include "stringBuiltin.hpp"
#include "stringsBuiltin.hpp"
#include "deblankBuiltin.hpp"
#include "convertStringsToCharsBuiltin.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
Expand Down Expand Up @@ -68,7 +69,10 @@ static const nlsGateway gateway[] = { { "char", Nelson::StringGateway::charBuilt
{ "deblank", Nelson::StringGateway::deblankBuiltin, 1, 1, CPP_BUILTIN_WITH_EVALUATOR },
{ "strlength", Nelson::StringGateway::strlengthBuiltin, 1, 1, CPP_BUILTIN_WITH_EVALUATOR },
{ "string", Nelson::StringGateway::stringBuiltin, 1, 1, CPP_BUILTIN_WITH_EVALUATOR },
{ "strings", Nelson::StringGateway::stringsBuiltin, 1, -1, CPP_BUILTIN_WITH_EVALUATOR } };
{ "strings", Nelson::StringGateway::stringsBuiltin, 1, -1, CPP_BUILTIN_WITH_EVALUATOR },
{ "convertStringsToChars", Nelson::StringGateway::convertStringsToCharsBuiltin, -1, -1,
CPP_BUILTIN_WITH_EVALUATOR },
};
//=============================================================================
NLSGATEWAYFUNC(gateway)
//=============================================================================
Expand Down
34 changes: 34 additions & 0 deletions modules/string/builtin/cpp/convertStringsToCharsBuiltin.cpp
@@ -0,0 +1,34 @@
//=============================================================================
// Copyright (c) 2016-2019 Allan CORNET (Nelson)
//=============================================================================
// LICENCE_BLOCK_BEGIN
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// LICENCE_BLOCK_END
//=============================================================================
#include "ConvertStringsToChars.hpp"
#include "Error.hpp"
#include "convertStringsToCharsBuiltin.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::StringGateway::convertStringsToCharsBuiltin(
Evaluator* eval, int nLhs, const ArrayOfVector& argIn)
{
if (nLhs > argIn.size()) {
Error(_W("Number of Input arguments must the same as output."));
}
return ConvertStringsToChars(argIn);
}
//=============================================================================
32 changes: 32 additions & 0 deletions modules/string/builtin/include/convertStringsToCharsBuiltin.hpp
@@ -0,0 +1,32 @@
//=============================================================================
// Copyright (c) 2016-2019 Allan CORNET (Nelson)
//=============================================================================
// LICENCE_BLOCK_BEGIN
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// LICENCE_BLOCK_END
//=============================================================================
#pragma once
//=============================================================================
#include "ArrayOf.hpp"
#include "Evaluator.hpp"
//=============================================================================
namespace Nelson {
//=============================================================================
namespace StringGateway {
ArrayOfVector
convertStringsToCharsBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn);
}
//=============================================================================
} // namespace Nelson
//=============================================================================
84 changes: 84 additions & 0 deletions modules/string/help/en_US/xml/convertStringsToChars.xml
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<xmldoc>
<copyright>SAME AS NELSON SOFTWARE</copyright>

<language>en_US</language>
<keyword>convertStringsToChars</keyword>
<short_description>Convert string arrays to character arrays.</short_description>

<syntax>
<syntax_item>C = convertStringsToChars(S)</syntax_item>
<syntax_item>[B1, B2, ..., BN] = convertStringsToChars(A1, A2, ..., AN)</syntax_item>
</syntax>

<param_input>
<param_input_item>
<param_name>S</param_name>
<param_description>if S is a string array, output C will be converted to an cell of strings or an character vector (if S is scalar).</param_description>
</param_input_item>
<param_input_item>
<param_name>A1, A2, ..., AN</param_name>
<param_description>variables to convert to char array if it is a string array.</param_description>
</param_input_item>
</param_input>

<param_output>

<param_output_item>
<param_name>C</param_name>
<param_description>a string array or unaltered variable</param_description>
</param_output_item>
<param_output_item>
<param_name>B1, B2, ..., BN</param_name>
<param_description>variables converted to char array if it is a string array.</param_description>
</param_output_item>

</param_output>

<description>
<p><b>convertStringsToChars</b> converts string arrays to character arrays.</p>
</description>


<used_function></used_function>
<bibliography></bibliography>

<examples>

<example_item>
<example_item_type>nelson</example_item_type>
<example_item_description></example_item_description>
<example_item_data><![CDATA[A = convertStringsToChars("Nelson")
A = convertStringsToChars(["Nelson", string(NaN)])]]>
</example_item_data>

</example_item>

</examples>

<see_also>
<see_also_item>
<link linkend="${data_structures}cellstr">cellstr</link>
</see_also_item>
<see_also_item>
<link linkend="${string}string">string</link>
</see_also_item>
<see_also_item>
<link linkend="${string}char">char</link>
</see_also_item>

</see_also>

<history>
<history_item>
<history_version>1.0.0</history_version>
<history_description>initial version</history_description>
</history_item>
</history>

<authors>
<author_item>Allan CORNET</author_item>
</authors>
</xmldoc>


2 changes: 2 additions & 0 deletions modules/string/src/c/nlsString.vcxproj
Expand Up @@ -164,6 +164,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\cpp\ConvertStringsToChars.cpp" />
<ClCompile Include="..\cpp\IntegerToString.cpp" />
<ClCompile Include="..\cpp\MatrixToString.cpp" />
<ClCompile Include="..\cpp\StringCompare.cpp" />
Expand All @@ -187,6 +188,7 @@
<ItemGroup>
<ClInclude Include="..\..\src\include\nlsString_exports.h" />
<ClInclude Include="..\..\src\include\ToChar.hpp" />
<ClInclude Include="..\include\ConvertStringsToChars.hpp" />
<ClInclude Include="..\include\IntegerToString.hpp" />
<ClInclude Include="..\include\MatrixToString.hpp" />
<ClInclude Include="..\include\StringCompare.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions modules/string/src/c/nlsString.vcxproj.filters
Expand Up @@ -72,6 +72,9 @@
<ClCompile Include="..\cpp\StringDeblank.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\ConvertStringsToChars.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\include\ToChar.hpp">
Expand Down Expand Up @@ -134,6 +137,9 @@
<ClInclude Include="..\include\StringDeblank.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\ConvertStringsToChars.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down
63 changes: 63 additions & 0 deletions modules/string/src/cpp/ConvertStringsToChars.cpp
@@ -0,0 +1,63 @@
//=============================================================================
// Copyright (c) 2016-2019 Allan CORNET (Nelson)
//=============================================================================
// LICENCE_BLOCK_BEGIN
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// LICENCE_BLOCK_END
//=============================================================================
#include "ConvertStringsToChars.hpp"
#include "Exception.hpp"
//=============================================================================
namespace Nelson {
ArrayOfVector
ConvertStringsToChars(const ArrayOfVector& A)
{
ArrayOfVector res;
for (auto value : A) {
if (value.isStringArray()) {
Dimensions dims = value.getDimensions();
if (dims.isEmpty(false)) {
ArrayOf valueAsCell = ArrayOf(NLS_CELL_ARRAY, dims, nullptr);
res.push_back(valueAsCell);
} else if (dims.isScalar()) {
auto* elementsStr = (ArrayOf*)value.getDataPointer();
ArrayOf element = elementsStr[0];
if (element.getDataClass() == NLS_CHAR) {
res.push_back(
ArrayOf::characterArrayConstructor(element.getContentAsWideString()));
} else {
res.push_back(ArrayOf::characterArrayConstructor(""));
}
} else {
auto* elementsCell = new_with_exception<ArrayOf>(dims.getElementCount(), false);
ArrayOf valueAsCell = ArrayOf(NLS_CELL_ARRAY, dims, elementsCell);
auto* elementsStr = (ArrayOf*)value.getDataPointer();
for (indexType q = 0; q < dims.getElementCount(); q++) {
if (elementsStr[q].getDataClass() == NLS_CHAR) {
elementsCell[q] = elementsStr[q];
} else {
elementsCell[q] = ArrayOf::characterArrayConstructor("");
}
}
res.push_back(valueAsCell);
}
} else {
res.push_back(value);
}
}
return res;
}
//=============================================================================
} // namespace Nelson
//=============================================================================
10 changes: 5 additions & 5 deletions modules/string/src/cpp/StringCompare.cpp
Expand Up @@ -43,7 +43,7 @@ compareString(std::wstring A, std::wstring B, bool bCaseSensitive, indexType len
return compareString(strA, strB, false);
}
if (bCaseSensitive) {
bEq = (A.compare(B) == 0);
bEq = (A == B);
} else {
bEq = boost::iequals(A, B);
}
Expand Down Expand Up @@ -99,8 +99,8 @@ StringCompare(ArrayOf A, ArrayOf B, bool bCaseSensitive, indexType len)
ArrayOf elementA = cellA[k];
ArrayOf elementB = cellB[k];
if (elementA.isRowVectorCharacterArray() && elementB.isRowVectorCharacterArray()) {
Cp[k] = compareString(elementA.getContentAsWideString(),
elementB.getContentAsWideString(), bCaseSensitive, len);
Cp[k] = static_cast<Nelson::logical>(compareString(elementA.getContentAsWideString(),
elementB.getContentAsWideString(), bCaseSensitive, len));
} else if (elementA.isCharacterArray() && elementB.isCharacterArray()) {
wstringVector s1 = elementA.getContentAsWideStringVector();
wstringVector s2 = elementB.getContentAsWideStringVector();
Expand Down Expand Up @@ -201,8 +201,8 @@ StringCompare(ArrayOf A, ArrayOf B, bool bCaseSensitive, indexType len)
} else {
ArrayOf elementA = cellA[k];
if (elementA.isCharacterArray() && scalar2.isCharacterArray()) {
Cp[k] = compareString(elementA.getContentAsWideString(),
scalar2.getContentAsWideString(), bCaseSensitive, len);
Cp[k] = static_cast<Nelson::logical>(compareString(elementA.getContentAsWideString(),
scalar2.getContentAsWideString(), bCaseSensitive, len));
} else {
Cp[k] = false;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/string/src/cpp/StringContains.cpp
Expand Up @@ -68,7 +68,7 @@ StringContains(ArrayOf A, ArrayOf Pattern, bool bCaseSensitive)
auto* cellA = (ArrayOf*)(A.getDataPointer());
for (size_t k = 0; k < nbA; k++) {
result[k]
= containsString(cellA[k].getContentAsWideString(), pattern, bCaseSensitive);
= static_cast<Nelson::logical>(containsString(cellA[k].getContentAsWideString(), pattern, bCaseSensitive));
}
res = ArrayOf(NLS_LOGICAL, dimA, result);
} else if ((A.isStringArray() || IsCellOfString(A))
Expand All @@ -85,7 +85,7 @@ StringContains(ArrayOf A, ArrayOf Pattern, bool bCaseSensitive)
bool val = containsString(cellA[k].getContentAsWideString(),
cellPattern[l].getContentAsWideString(), bCaseSensitive);
if (val) {
result[k] = val;
result[k] = static_cast<Nelson::logical>(val);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/string/src/cpp/StringEndsWith.cpp
Expand Up @@ -69,7 +69,7 @@ StringEndsWith(ArrayOf A, ArrayOf Pattern, bool bCaseSensitive)
auto* cellA = (ArrayOf*)(A.getDataPointer());
for (size_t k = 0; k < nbA; k++) {
result[k]
= endsWithString(cellA[k].getContentAsWideString(), pattern, bCaseSensitive);
= static_cast<Nelson::logical>(endsWithString(cellA[k].getContentAsWideString(), pattern, bCaseSensitive));
}
res = ArrayOf(NLS_LOGICAL, dimA, result);
} else if ((A.isStringArray() || IsCellOfString(A))
Expand All @@ -86,7 +86,7 @@ StringEndsWith(ArrayOf A, ArrayOf Pattern, bool bCaseSensitive)
bool val = endsWithString(cellA[k].getContentAsWideString(),
cellPattern[l].getContentAsWideString(), bCaseSensitive);
if (val) {
result[k] = val;
result[k] = static_cast<Nelson::logical>(val);
break;
}
}
Expand Down

0 comments on commit 8725ab7

Please sign in to comment.