-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add zapi::internal::parse_namespaces function
- Loading branch information
1 parent
7c6ff22
commit e432b82
Showing
7 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @copyright 2017-2018 zzu_softboy <zzu_softboy@163.com> | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
// Created by softboy on 2017/11/01. | ||
|
||
#ifndef ZAPI_UTILS_INTERNAL_FUNCS_H | ||
#define ZAPI_UTILS_INTERNAL_FUNCS_H | ||
|
||
#include <list> | ||
#include <string> | ||
|
||
namespace zapi | ||
{ | ||
namespace internal | ||
{ | ||
|
||
bool parse_namespaces(const std::string &ns, std::list<std::string> &parts); | ||
|
||
} // internal | ||
} // zapi | ||
|
||
#endif // ZAPI_UTILS_INTERNAL_FUNCS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @copyright 2017-2018 zzu_softboy <zzu_softboy@163.com> | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
// Created by softboy on 2017/11/01. | ||
|
||
#include "zapi/utils/InternalFuncs.h" | ||
|
||
namespace zapi | ||
{ | ||
namespace internal | ||
{ | ||
|
||
bool parse_namespaces(const std::string &ns, std::list<std::string> &parts) | ||
{ | ||
int markPos = 0; | ||
int cycleSepCount = 0; | ||
std::string::value_type curChar; | ||
std::string::size_type len = ns.size(); | ||
if (0 == len) { | ||
return true; | ||
} | ||
if (ns[len - 1] == ':') { | ||
return false; | ||
} | ||
int i = 0; | ||
for (; i < ns.size(); i++) { | ||
curChar = ns[i]; | ||
if (':' == curChar) { | ||
if (cycleSepCount == 0 && (i - markPos > 0)) { | ||
parts.push_back(ns.substr(markPos, i - markPos)); | ||
} | ||
cycleSepCount++; | ||
} else { | ||
if (cycleSepCount == 1 || cycleSepCount > 2) { | ||
parts.clear(); | ||
return false; | ||
} else if (cycleSepCount == 2) { | ||
markPos = i; | ||
cycleSepCount = 0; | ||
} | ||
} | ||
} | ||
if (i > markPos) { | ||
parts.push_back(ns.substr(markPos, i - markPos)); | ||
} | ||
return true; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
set(UTILS_TEST_SRCS | ||
InternalTest.cpp) | ||
zapi_add_unittest(UnitTests UtilsTest ${UTILS_TEST_SRCS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// @copyright 2017-2018 zzu_softboy <zzu_softboy@163.com> | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
// Created by softboy on 2017/11/01. | ||
|
||
#include "php/sapi/embed/php_embed.h" | ||
#include "gtest/gtest.h" | ||
#include "zapi/utils/InternalFuncs.h" | ||
#include <string> | ||
#include <list> | ||
|
||
using zapi::internal::parse_namespaces; | ||
|
||
TEST(UtilsInternalTest, testParseNamespace) | ||
{ | ||
std::list<std::string> parts; | ||
std::string ns("::::"); | ||
bool parseStatus = true; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_FALSE(parseStatus); | ||
ns = "zapi::"; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_FALSE(parseStatus); | ||
ns = "zapi:kernel"; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_FALSE(parseStatus); | ||
ns = "zapi::kernel:io"; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_FALSE(parseStatus); | ||
ns = "zapi"; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_TRUE(parseStatus); | ||
ASSERT_EQ(parts, std::list<std::string>{"zapi"}); | ||
parts.clear(); | ||
ns = "zapi::kernel::io"; | ||
parseStatus = parse_namespaces(ns, parts); | ||
ASSERT_TRUE(parseStatus); | ||
ASSERT_EQ(parts, std::list<std::string>({"zapi", "kernel", "io"})); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int retCode = 0; | ||
PHP_EMBED_START_BLOCK(argc,argv); | ||
::testing::InitGoogleTest(&argc, argv); | ||
retCode = RUN_ALL_TESTS(); | ||
PHP_EMBED_END_BLOCK(); | ||
return retCode; | ||
} |