Skip to content

Commit

Permalink
add zapi::internal::parse_namespaces function
Browse files Browse the repository at this point in the history
  • Loading branch information
zzusoftboy committed Nov 1, 2017
1 parent 7c6ff22 commit e432b82
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(ZAPI_PUBLIC_HEADERS
${ZAPI_INCLUDE_DIR}/zapi/ds/internal/ArrayItemProxyPrivate.h
${ZAPI_INCLUDE_DIR}/zapi/utils/PhpFuncs.h
${ZAPI_INCLUDE_DIR}/zapi/utils/CommonFuncs.h
${ZAPI_INCLUDE_DIR}/zapi/utils/InternalFuncs.h
${ZAPI_INCLUDE_DIR}/zapi/vm/AbstractClass.h
${ZAPI_INCLUDE_DIR}/zapi/vm/InvokeBridge.h
${ZAPI_INCLUDE_DIR}/zapi/vm/Callable.h
Expand Down
32 changes: 32 additions & 0 deletions include/zapi/utils/InternalFuncs.h
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
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ set(ZAPI_SRC
vm/ObjectBinder.cpp
vm/Closure.cpp
utils/PhpFuncs.cpp
utils/CommonFuncs.cpp)
utils/CommonFuncs.cpp
utils/InternalFuncs.cpp
)

zapi_add_library(${ZAPI_PROJECT_NAME} SHARED ${ZAPI_SRC} ${ZAPI_PUBLIC_HEADERS})

Expand Down
60 changes: 60 additions & 0 deletions src/utils/InternalFuncs.cpp
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;
}

}
}
1 change: 1 addition & 0 deletions unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ add_subdirectory(ds)
add_subdirectory(extension)
add_subdirectory(vm)
add_subdirectory(std)
add_subdirectory(utils)
zapi_add_unittest(UnitTests EmbedPhpTest EmbedPhpTest.cpp)
3 changes: 3 additions & 0 deletions unittests/utils/CMakeLists.txt
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})
59 changes: 59 additions & 0 deletions unittests/utils/InternalTest.cpp
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;
}

0 comments on commit e432b82

Please sign in to comment.