Skip to content

Commit 075b8a1

Browse files
authored
Changed usage of std::vector to std::list (#15)
* Changed usage of std::vector to std::list * ran clang-format * added formatting to README.md
1 parent 140cd9a commit 075b8a1

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ Yash is a C++17 header-only minimal shell for embedded devices with support for
1111
```cpp
1212
#include <Yash.h>
1313

14-
void i2cRead(const std::vector<std::string>& args)
14+
void i2cRead(Yash::Arguments args)
1515
{
16-
printf("i2cRead(%s, %s, %s)\n", args.at(0).c_str(), args.at(1).c_str(), args.at(2).c_str());
16+
17+
printf("i2cRead(%s, %s, %s)\n",
18+
std::next(args.begin(), 0)->c_str(),
19+
std::next(args.begin(), 1)->c_str(),
20+
std::next(args.begin(), 2)->c_str());
1721
}
1822

19-
void i2cWrite(const std::vector<std::string>& args)
23+
void i2cWrite(Yash::Arguments args)
2024
{
21-
printf("i2cWrite(%s, %s, %s)\n", args.at(0).c_str(), args.at(1).c_str(), args.at(2).c_str());
25+
printf("i2cWrite(%s, %s, %s)\n",
26+
std::next(args.begin(), 0)->c_str(),
27+
std::next(args.begin(), 1)->c_str(),
28+
std::next(args.begin(), 2)->c_str());
2229
}
2330

24-
void info(const std::vector<std::string>&)
31+
void info(Yash::Arguments)
2532
{
2633
printf("info()\n");
2734
}

example/example.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33

44
#include <Yash.h>
55
#include <con.h>
6-
#include <stdlib.h>
6+
#include <cstdlib>
77

8-
void i2cRead(const std::vector<std::string>& args)
8+
9+
void i2cRead(Yash::Arguments args)
910
{
10-
printf("i2cRead(%s, %s, %s)\n", args.at(0).c_str(), args.at(1).c_str(), args.at(2).c_str());
11+
12+
printf("i2cRead(%s, %s, %s)\n",
13+
std::next(args.begin(), 0)->c_str(),
14+
std::next(args.begin(), 1)->c_str(),
15+
std::next(args.begin(), 2)->c_str());
1116
}
1217

13-
void i2cWrite(const std::vector<std::string>& args)
18+
void i2cWrite(Yash::Arguments args)
1419
{
15-
printf("i2cWrite(%s, %s, %s)\n", args.at(0).c_str(), args.at(1).c_str(), args.at(2).c_str());
20+
printf("i2cWrite(%s, %s, %s)\n",
21+
std::next(args.begin(), 0)->c_str(),
22+
std::next(args.begin(), 1)->c_str(),
23+
std::next(args.begin(), 2)->c_str());
1624
}
1725

18-
void info(const std::vector<std::string>&)
26+
void info(Yash::Arguments)
1927
{
2028
printf("info()\n");
2129
}

include/Yash.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
#include <cstdio>
88
#include <cstring>
99
#include <functional>
10+
#include <list>
1011
#include <map>
1112
#include <numeric>
1213
#include <string>
1314
#include <string_view>
14-
#include <vector>
1515

1616
namespace Yash {
1717

18-
typedef void (*CommandFunction)(const std::vector<std::string>&);
18+
typedef const std::list<std::string>& Arguments;
19+
typedef void (*CommandFunction)(Yash::Arguments);
1920

2021
struct Command {
2122
std::string_view name;
@@ -277,7 +278,7 @@ class Yash {
277278

278279
void runCommand()
279280
{
280-
std::vector<std::string> arguments;
281+
std::list<std::string> arguments;
281282
for (const auto& command : m_commands) {
282283
if (!m_command.compare(0, command.name.size(), command.name)) {
283284
auto args = m_command.substr(command.name.size());
@@ -380,8 +381,8 @@ class Yash {
380381
CtrlState m_ctrlState { CtrlState::None };
381382
const std::array<Command, TCommandArraySize>& m_commands;
382383
std::function<void(const char*)> m_printFunction;
383-
std::vector<std::string> m_commandHistory;
384-
std::vector<std::string>::const_iterator m_commandHistoryIndex;
384+
std::list<std::string> m_commandHistory;
385+
std::list<std::string>::const_iterator m_commandHistoryIndex;
385386
std::string m_command;
386387
std::string m_prompt;
387388
std::string m_ctrlCharacter;

test/TestYash.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55
#define private public
66
#include "Yash.h"
77

8-
#define SetupHistoryPreconditions() \
9-
MOCK_EXPECT(print); \
10-
MOCK_EXPECT(i2c).once(); \
8+
#define SetupHistoryPreconditions() \
9+
MOCK_EXPECT(print); \
10+
MOCK_EXPECT(i2c).once(); \
1111
for (char& character : "i2c read 1 2 3\n"s) \
12-
yash.setCharacter(character); \
13-
MOCK_EXPECT(info).once(); \
14-
for (char& character : "info\n"s) \
12+
yash.setCharacter(character); \
13+
MOCK_EXPECT(info).once(); \
14+
for (char& character : "info\n"s) \
1515
yash.setCharacter(character);
1616

1717
using namespace std::string_literals;
1818

1919
namespace {
2020
MOCK_FUNCTION(print, 1, void(const char*));
21-
MOCK_FUNCTION(i2c, 1, void(const std::vector<std::string>& args));
22-
MOCK_FUNCTION(info, 1, void(const std::vector<std::string>& args));
21+
MOCK_FUNCTION(i2c, 1, void(Yash::Arguments args));
22+
MOCK_FUNCTION(info, 1, void(Yash::Arguments args));
2323

24-
constexpr const char *s_clearCharacter = "\033[1D \033[1D";
25-
constexpr const char *s_moveCursorForward = "\033[1C";
26-
constexpr const char *s_moveCursorBackward = "\033[1D";
24+
constexpr const char* s_clearCharacter = "\033[1D \033[1D";
25+
constexpr const char* s_moveCursorForward = "\033[1C";
26+
constexpr const char* s_moveCursorBackward = "\033[1D";
2727
} // namespace
2828

2929

3030
TEST_CASE("Yash test")
3131
{
3232
static constexpr std::array<Yash::Command, 2> commands {
3333
{ { "i2c read", "I2C read <addr> <reg> <bytes>", &i2c, 3 },
34-
{ "info", "System info", &info, 0 } }
34+
{ "info", "System info", &info, 0 } }
3535
};
3636

3737
std::string prompt = "$ ";
@@ -128,7 +128,7 @@ TEST_CASE("Yash test")
128128
{
129129
std::string testCommand = "i2c read 1 2 3\n";
130130

131-
MOCK_EXPECT(i2c).once().with(std::vector<std::string> { "1", "2", "3" });
131+
MOCK_EXPECT(i2c).once().with(std::list<std::string> { "1", "2", "3" });
132132
MOCK_EXPECT(print);
133133

134134
for (char& character : testCommand)
@@ -142,7 +142,7 @@ TEST_CASE("Yash test")
142142
"i2c read 1 2 3 \n",
143143
"i2c read 1 2 3 \n") };
144144

145-
MOCK_EXPECT(i2c).once().with(std::vector<std::string> { "1", "2", "3" });
145+
MOCK_EXPECT(i2c).once().with(std::list<std::string> { "1", "2", "3" });
146146
MOCK_EXPECT(print);
147147

148148
for (char& character : testCommand)

0 commit comments

Comments
 (0)