Skip to content

Commit 866674a

Browse files
committed
UPD | slices
1 parent d663358 commit 866674a

File tree

6 files changed

+302
-59
lines changed

6 files changed

+302
-59
lines changed

include/std/ManapiSlice.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace manapi {
4343
*/
4444
void *buffer ();
4545

46+
std::string_view operator *();
47+
4648
/**
4749
* get the storage buffer size
4850
* @return the storage buffer size
@@ -56,6 +58,7 @@ namespace manapi {
5658
MANAPIHTTP_NODISCARD bool is_last () const;
5759
private:
5860
slice_part_t *part;
61+
5962
slice_base *base_;
6063
};
6164

@@ -74,6 +77,8 @@ namespace manapi {
7477

7578
slice_const_iterator &operator++(int);
7679

80+
std::string_view operator *();
81+
7782
bool operator==(const slice_const_iterator &n) const;
7883

7984
/**

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ int main() {
9898
co_return resp.file("../test.html").unwrap();
9999
}).unwrap();
100100

101+
102+
route.GET ("/", "../utils").unwrap();
103+
101104
route.POST ("/", [db] (http::req &req, http::resp &resp) mutable -> manapi::future<> {
102105
std::string name;
103106
manapi::unwrap(co_await req.form([&name] (std::string key) {

package/DEBIAN/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Installed-Size:
66
Maintainer: Xiadnoring <me@timurtuty.ru>
77
Homepage: https://github.com/xiadnoring/manapi-http
88
Architecture: amd64
9-
Depends: libstdc++6,libuv1-dev,libbrotli-dev,libzstd-dev,libgmp-dev,libcurl4-openssl-dev,libnghttp2-dev,libssl-dev
9+
Depends: libstdc++6,libuv1-dev,libbrotli-dev,libzstd-dev,libgmp-dev,libcurl4-openssl-dev,libnghttp2-dev,libssl-dev,zlib1g-dev
1010
Description: HTTP server to easy develop API-services and Web Applications.
1111
HTTP server to easy develop API-services and Web Applications.

src/std/ManapiSlice.cpp

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ manapi::slice_iterator & manapi::slice_iterator::operator++(int) {
8585
return *this;
8686
}
8787

88+
std::string_view manapi::slice_iterator::operator*() {
89+
return std::string_view (static_cast<const char *> (this->buffer()), this->size ());
90+
}
91+
8892
bool manapi::slice_iterator::operator==(const slice_iterator &n) const {
8993
return this->part == n.part;
9094
}
@@ -120,6 +124,10 @@ manapi::slice_const_iterator & manapi::slice_const_iterator::operator=(const sli
120124

121125
manapi::slice_const_iterator::~slice_const_iterator() = default;
122126

127+
std::string_view manapi::slice_const_iterator::operator*() {
128+
return std::string_view (static_cast<const char *> (this->buffer()), this->size ());
129+
}
130+
123131
manapi::slice_const_iterator & manapi::slice_const_iterator::operator++() {
124132
this->part = this->part->next;
125133
return *this;
@@ -430,90 +438,86 @@ manapi::slice_const_iterator manapi::slice_base::end() const {
430438

431439
int manapi::slice_base::cmp(const manapi::slice_base &n) const MANAPIHTTP_NOEXCEPT {
432440
auto size = n.size();
433-
if (this->size() != size)
434-
return this->size() > size ? -1 : 1;
435441

436442
auto buffptr1 = this->first;
437443
auto buffptr2 = n.first;
438444

439445
std::size_t cursor1 = this->shift();
440446
std::size_t cursor2 = n.shift();
441447

442-
std::size_t size1 = buffptr1->buff.len;
443-
std::size_t size2 = buffptr2->buff.len;
448+
if (buffptr1 && buffptr2) {
449+
std::size_t size1 = buffptr1->buff.len;
450+
std::size_t size2 = buffptr2->buff.len;
444451

445-
if (!buffptr1->next)
446-
size1 -= this->rshift_;
452+
if (!buffptr1->next)
453+
size1 -= this->rshift_;
447454

448-
if (!buffptr2->next)
449-
size2 -= n.rshift_;
455+
if (!buffptr2->next)
456+
size2 -= n.rshift_;
450457

451-
while (size) {
452-
assert(buffptr1 && buffptr2);
458+
while (size && buffptr1 && buffptr2) {
459+
auto cmp_size = std::min<std::size_t>(size1 - cursor1,
460+
size2 - cursor2);
453461

454-
auto const cmp_size = std::min<std::size_t>(size1 - cursor1,
455-
size2 - cursor2);
462+
if (cmp_size > size)
463+
cmp_size = size;
456464

457-
if (auto const rhs = strncmp(buffptr1->buff.base + cursor1, buffptr2->buff.base + cursor2, cmp_size))
458-
return rhs > 0 ? 1 : -1;
465+
if (auto const rhs = memcmp(buffptr1->buff.base + cursor1, buffptr2->buff.base + cursor2, cmp_size))
466+
return rhs > 0 ? 1 : -1;
459467

460-
cursor1 += cmp_size;
461-
cursor2 += cmp_size;
468+
cursor1 += cmp_size;
469+
cursor2 += cmp_size;
462470

463-
if (cursor1 == buffptr1->buff.len) {
464-
buffptr1 = buffptr1->next;
465-
cursor1 = 0;
471+
if (cursor1 == buffptr1->buff.len) {
472+
buffptr1 = buffptr1->next;
473+
cursor1 = 0;
466474

467-
if (buffptr1) {
468-
size1 = buffptr1->buff.len;
469-
if (!buffptr1->next)
470-
size1 -= this->rshift_;
475+
if (buffptr1) {
476+
size1 = buffptr1->buff.len;
477+
if (!buffptr1->next)
478+
size1 -= this->rshift_;
479+
}
471480
}
472-
}
473481

474-
if (cursor2 == buffptr2->buff.len) {
475-
buffptr2 = buffptr2->next;
476-
cursor2 = 0;
482+
if (cursor2 == buffptr2->buff.len) {
483+
buffptr2 = buffptr2->next;
484+
cursor2 = 0;
477485

478-
if (buffptr2) {
479-
size2 = buffptr2->buff.len;
480-
if (!buffptr2->next)
481-
size2 -= n.rshift_;
486+
if (buffptr2) {
487+
size2 = buffptr2->buff.len;
488+
if (!buffptr2->next)
489+
size2 -= n.rshift_;
490+
}
482491
}
483-
}
484492

485-
assert(size >= cmp_size);
486-
size -= cmp_size;
487-
};
493+
size -= cmp_size;
494+
}
495+
}
488496

497+
if (this->size() == size)
498+
return 0;
489499

490-
return 0;
500+
return this->size() > size ? 1 : -1;
491501
}
492502

493503
int manapi::slice_base::cmp(void *data, std::size_t size) const MANAPIHTTP_NOEXCEPT {
494-
auto const s = this->size ();
495-
if (s == size) {
496-
if (!s)
497-
return 0;
498-
int rhs;
499-
size_t ss;
500-
for (auto it = this->begin(); it != this->end(); it++) {
501-
assert(size);
502-
ss = std::min<std::size_t>(it.size(), size);
503-
rhs = memcmp (it.buffer(), data, ss);
504-
if (rhs)
505-
return rhs;
506-
size -= ss;
507-
data = static_cast<char*>(data) + ss;
508-
}
509-
}
510-
else {
511-
if (s > size)
512-
return -1;
513-
return 1;
504+
int rhs;
505+
size_t ss;
506+
for (auto it = this->begin(); it != this->end(); it++) {
507+
ss = std::min<std::size_t>(it.size(), size);
508+
rhs = memcmp (static_cast<const char *>(it.buffer()), static_cast<const char *>(data), ss);
509+
if (rhs)
510+
return rhs;
511+
size -= ss;
512+
data = static_cast<char*>(data) + ss;
513+
if (it.size() != ss)
514+
return 1;
514515
}
515516

516-
return 0;
517+
if (!size)
518+
return 0;
519+
520+
return -1;
517521
}
518522

519523
void manapi::slice_base::slices_buffs(ev::buff_t *buffs) const {

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_compile_options($<$<CXX_COMPILER_ID:GNU>:-fmerge-all-constants>)
66

77
set(MANAPIHTTP_TEST_TOOL_FILES "utest.h" "tools.hpp")
88

9-
set(MANAPIHTTP_TESTS_SOURCES test_std.cpp test_bigint.cpp test_timer.cpp test_fs.cpp test_fs_io.cpp test_http_and_fetch.cpp test_json.cpp test_json_masks.cpp test_parse.cpp test_http_and_fetch_keep_alive.cpp test_function.cpp test_cache.cpp)
9+
set(MANAPIHTTP_TESTS_SOURCES test_std.cpp test_bigint.cpp test_timer.cpp test_fs.cpp test_fs_io.cpp test_http_and_fetch.cpp test_json.cpp test_json_masks.cpp test_parse.cpp test_slice.cpp test_http_and_fetch_keep_alive.cpp test_function.cpp test_cache.cpp)
1010
foreach(test ${MANAPIHTTP_TESTS_SOURCES})
1111
set(test_name ${test}_ctest)
1212
include_directories(${test_name} "../include" "${CMAKE_BINARY_DIR}/include")

0 commit comments

Comments
 (0)