Skip to content

Commit

Permalink
Cleanup struct string
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Jun 17, 2024
1 parent bbe7e51 commit aa28fb3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 95 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.203_amd64.deb
sudo apt install build/meevax_0.5.204_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.203.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.204.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.203_amd64.deb`
| `package` | Generate debian package `meevax_0.5.204_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.203
0.5.204
31 changes: 18 additions & 13 deletions include/meevax/kernel/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ namespace meevax
{
inline namespace kernel
{
struct string
struct string : private std::vector<character>
{
std::vector<character> vector;

string() = default;
using std::vector<character>::at;
using std::vector<character>::begin;
using std::vector<character>::emplace_back;
using std::vector<character>::end;
using std::vector<character>::insert;
using std::vector<character>::push_back;
using std::vector<character>::rbegin;
using std::vector<character>::rend;
using std::vector<character>::reserve;
using std::vector<character>::size;
using std::vector<character>::vector;

explicit string(std::string const&);

explicit string(std::size_t const, character const&);

template <typename Iterator>
explicit string(Iterator begin, Iterator end)
: vector { begin, end }
{}

operator std::string() const;
};

auto operator ==(string const&, string const&) -> bool;
friend auto operator ==(string const& lhs, string const& rhs) { return static_cast<std::vector<character> const&>(lhs) == static_cast<std::vector<character> const&>(rhs); }
friend auto operator < (string const& lhs, string const& rhs) { return static_cast<std::vector<character> const&>(lhs) < static_cast<std::vector<character> const&>(rhs); }
friend auto operator > (string const& lhs, string const& rhs) { return static_cast<std::vector<character> const&>(lhs) > static_cast<std::vector<character> const&>(rhs); }
friend auto operator <=(string const& lhs, string const& rhs) { return static_cast<std::vector<character> const&>(lhs) <= static_cast<std::vector<character> const&>(rhs); }
friend auto operator >=(string const& lhs, string const& rhs) { return static_cast<std::vector<character> const&>(lhs) >= static_cast<std::vector<character> const&>(rhs); }
};

auto operator <<(std::ostream &, string const&) -> std::ostream &;
} // namespace kernel
Expand Down
108 changes: 54 additions & 54 deletions src/kernel/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,32 +1396,32 @@ inline namespace kernel

for (let const& x : xs)
{
s.as<string>().vector.push_back(x.as<character>());
s.as<string>().push_back(x.as<character>());
}

return s;
});

library.define<procedure>("string-length", [](let const& xs)
{
return make<exact_integer>(car(xs).as<string>().vector.size());
return make<exact_integer>(car(xs).as<string>().size());
});

library.define<procedure>("string-ref", [](let const& xs)
{
return make(car(xs).as<string>().vector.at(cadr(xs).as<exact_integer>()));
return make(car(xs).as<string>().at(cadr(xs).as<exact_integer>()));
});

library.define<procedure>("string-set!", [](let & xs)
{
car(xs).as<string>().vector.at(cadr(xs).as<exact_integer>()) = caddr(xs).as<character>();
car(xs).as<string>().at(cadr(xs).as<exact_integer>()) = caddr(xs).as<character>();
});

library.define<procedure>("string=?", [](let const& xs)
{
auto compare = [](let const& a, let const& b)
{
return not (a.as<string>().vector == b.as<string>().vector);
return not (a.as<string>() == b.as<string>());
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1431,7 +1431,7 @@ inline namespace kernel
{
auto compare = [](let const& a, let const& b)
{
return not (a.as<string>().vector < b.as<string>().vector);
return not (a.as<string>() < b.as<string>());
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1441,7 +1441,7 @@ inline namespace kernel
{
auto compare = [](let const& a, let const& b)
{
return not (a.as<string>().vector > b.as<string>().vector);
return not (a.as<string>() > b.as<string>());
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1451,7 +1451,7 @@ inline namespace kernel
{
auto compare = [](let const& a, let const& b)
{
return not (a.as<string>().vector <= b.as<string>().vector);
return not (a.as<string>() <= b.as<string>());
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1461,7 +1461,7 @@ inline namespace kernel
{
auto compare = [](let const& a, let const& b)
{
return not (a.as<string>().vector >= b.as<string>().vector);
return not (a.as<string>() >= b.as<string>());
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1476,8 +1476,8 @@ inline namespace kernel
return c1.downcase() == c2.downcase();
};

return not std::lexicographical_compare(s1.as<string>().vector.begin(), s1.as<string>().vector.end(),
s2.as<string>().vector.begin(), s2.as<string>().vector.end(), compare);
return not std::lexicographical_compare(s1.as<string>().begin(), s1.as<string>().end(),
s2.as<string>().begin(), s2.as<string>().end(), compare);
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1492,8 +1492,8 @@ inline namespace kernel
return c1.downcase() < c2.downcase();
};

return not std::lexicographical_compare(s1.as<string>().vector.begin(), s1.as<string>().vector.end(),
s2.as<string>().vector.begin(), s2.as<string>().vector.end(), compare);
return not std::lexicographical_compare(s1.as<string>().begin(), s1.as<string>().end(),
s2.as<string>().begin(), s2.as<string>().end(), compare);
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1508,8 +1508,8 @@ inline namespace kernel
return c1.downcase() > c2.downcase();
};

return not std::lexicographical_compare(s1.as<string>().vector.begin(), s1.as<string>().vector.end(),
s2.as<string>().vector.begin(), s2.as<string>().vector.end(), compare);
return not std::lexicographical_compare(s1.as<string>().begin(), s1.as<string>().end(),
s2.as<string>().begin(), s2.as<string>().end(), compare);
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1524,8 +1524,8 @@ inline namespace kernel
return c1.downcase() <= c2.downcase();
};

return not std::lexicographical_compare(s1.as<string>().vector.begin(), s1.as<string>().vector.end(),
s2.as<string>().vector.begin(), s2.as<string>().vector.end(), compare);
return not std::lexicographical_compare(s1.as<string>().begin(), s1.as<string>().end(),
s2.as<string>().begin(), s2.as<string>().end(), compare);
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1540,8 +1540,8 @@ inline namespace kernel
return c1.downcase() >= c2.downcase();
};

return not std::lexicographical_compare(s1.as<string>().vector.begin(), s1.as<string>().vector.end(),
s2.as<string>().vector.begin(), s2.as<string>().vector.end(), compare);
return not std::lexicographical_compare(s1.as<string>().begin(), s1.as<string>().end(),
s2.as<string>().begin(), s2.as<string>().end(), compare);
};

return std::adjacent_find(xs.begin(), xs.end(), compare) == xs.end();
Expand All @@ -1553,9 +1553,9 @@ inline namespace kernel

for (let const& x : xs)
{
s.as<string>().vector.insert(s.as<string>().vector.end(),
x.as<string>().vector.begin(),
x.as<string>().vector.end());
s.as<string>().insert(s.as<string>().end(),
x.as<string>().begin(),
x.as<string>().end());
}

return s;
Expand All @@ -1571,22 +1571,22 @@ inline namespace kernel
switch (length(xs))
{
case 1:
return std::accumulate(car(xs).as<string>().vector.rbegin(),
car(xs).as<string>().vector.rend(),
return std::accumulate(car(xs).as<string>().rbegin(),
car(xs).as<string>().rend(),
unit,
push);

case 2:
return std::accumulate(car(xs).as<string>().vector.rbegin(),
std::prev(car(xs).as<string>().vector.rend(),
return std::accumulate(car(xs).as<string>().rbegin(),
std::prev(car(xs).as<string>().rend(),
cadr(xs).as<exact_integer>()),
unit,
push);

case 3:
return std::accumulate(std::prev(car(xs).as<string>().vector.rend(),
return std::accumulate(std::prev(car(xs).as<string>().rend(),
caddr(xs).as<exact_integer>()),
std::prev(car(xs).as<string>().vector.rend(),
std::prev(car(xs).as<string>().rend(),
cadr(xs).as<exact_integer>()),
unit,
push);
Expand All @@ -1602,7 +1602,7 @@ inline namespace kernel

for (let const& x : car(xs))
{
s.as<string>().vector.push_back(x.as<character>());
s.as<string>().push_back(x.as<character>());
}

return s;
Expand All @@ -1613,18 +1613,18 @@ inline namespace kernel
switch (length(xs))
{
case 1:
return make<string>(car(xs).as<string>().vector.begin(),
car(xs).as<string>().vector.end());
return make<string>(car(xs).as<string>().begin(),
car(xs).as<string>().end());

case 2:
return make<string>(std::next(car(xs).as<string>().vector.begin(),
return make<string>(std::next(car(xs).as<string>().begin(),
cadr(xs).as<exact_integer>()),
car(xs).as<string>().vector.end());
car(xs).as<string>().end());

case 3:
return make<string>(std::next(car(xs).as<string>().vector.begin(),
return make<string>(std::next(car(xs).as<string>().begin(),
cadr(xs).as<exact_integer>()),
std::next(car(xs).as<string>().vector.begin(),
std::next(car(xs).as<string>().begin(),
caddr(xs).as<exact_integer>()));

default:
Expand All @@ -1634,32 +1634,32 @@ inline namespace kernel

library.define<procedure>("string-copy!", [](let const& xs)
{
car(xs).as<string>().vector.reserve(car(xs).as<string>().vector.size() +
caddr(xs).as<string>().vector.size());
car(xs).as<string>().reserve(car(xs).as<string>().size() +
caddr(xs).as<string>().size());

switch (length(xs))
{
case 3:
std::copy(caddr(xs).as<string>().vector.begin(),
caddr(xs).as<string>().vector.end(),
std::next(car(xs).as<string>().vector.begin(),
std::copy(caddr(xs).as<string>().begin(),
caddr(xs).as<string>().end(),
std::next(car(xs).as<string>().begin(),
cadr(xs).as<exact_integer>()));
break;

case 4:
std::copy(std::next(caddr(xs).as<string>().vector.begin(),
std::copy(std::next(caddr(xs).as<string>().begin(),
cadddr(xs).as<exact_integer>()),
caddr(xs).as<string>().vector.end(),
std::next(car(xs).as<string>().vector.begin(),
caddr(xs).as<string>().end(),
std::next(car(xs).as<string>().begin(),
cadr(xs).as<exact_integer>()));
break;

case 5:
std::copy(std::next(caddr(xs).as<string>().vector.begin(),
std::copy(std::next(caddr(xs).as<string>().begin(),
cadddr(xs).as<exact_integer>()),
std::next(caddr(xs).as<string>().vector.begin(),
std::next(caddr(xs).as<string>().begin(),
caddddr(xs).as<exact_integer>()),
std::next(car(xs).as<string>().vector.begin(),
std::next(car(xs).as<string>().begin(),
cadr(xs).as<exact_integer>()));
break;

Expand All @@ -1673,22 +1673,22 @@ inline namespace kernel
switch (length(xs))
{
case 2:
std::fill(car(xs).as<string>().vector.begin(),
car(xs).as<string>().vector.end(),
std::fill(car(xs).as<string>().begin(),
car(xs).as<string>().end(),
cadr(xs).as<character>());
break;

case 3:
std::fill(std::next(car(xs).as<string>().vector.begin(),
std::fill(std::next(car(xs).as<string>().begin(),
caddr(xs).as<exact_integer>()),
car(xs).as<string>().vector.end(),
car(xs).as<string>().end(),
cadr(xs).as<character>());
break;

case 4:
std::fill(std::next(car(xs).as<string>().vector.begin(),
std::fill(std::next(car(xs).as<string>().begin(),
caddr(xs).as<exact_integer>()),
std::next(car(xs).as<string>().vector.begin(),
std::next(car(xs).as<string>().begin(),
cadddr(xs).as<exact_integer>()),
cadr(xs).as<character>());
break;
Expand Down Expand Up @@ -1882,7 +1882,7 @@ inline namespace kernel

auto push_back = [&](let const& x)
{
s.as<string>().vector.push_back(x.as<character>());
s.as<string>().push_back(x.as<character>());
};

switch (length(xs))
Expand Down Expand Up @@ -1917,7 +1917,7 @@ inline namespace kernel
{
let v = make<vector>();

for (auto character : car(xs).as<string>().vector)
for (auto character : car(xs).as<string>())
{
v.as<vector>().vector.push_back(make(character));
}
Expand Down
Loading

0 comments on commit aa28fb3

Please sign in to comment.