1

add string demo

This commit is contained in:
2022-07-24 01:35:34 +02:00
parent 281228b6be
commit adae600809
3 changed files with 75 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include "user/demo/PCSPKdemo.h"
#include "user/demo/PreemptiveThreadDemo.h"
#include "user/demo/SmartPointerDemo.h"
#include "user/demo/StringDemo.h"
#include "user/demo/TextDemo.h"
#include "user/demo/VBEdemo.h"
#include "user/demo/VectorDemo.h"
@ -33,7 +34,7 @@ void MainMenu::run() {
while (running) {
input = this->listener.waitForKeyEvent();
if (input >= '0' && input <= '9') {
if ((input >= '0' && input <= '9') || input == '!') {
switch (input) {
case '1':
running_demo = scheduler.ready<TextDemo>();
@ -66,6 +67,9 @@ void MainMenu::run() {
case '0':
running_demo = scheduler.ready<SmartPointerDemo>();
break;
case '!':
running_demo = scheduler.ready<StringDemo>();
break;
}
} else if (input == 'k') {
scheduler.nice_kill(running_demo); // NOTE: If thread exits itself this will throw error

View File

@ -0,0 +1,54 @@
#include "user/demo/StringDemo.h"
#include "kernel/Globals.h"
#include "user/lib/String.h"
void StringDemo::run() {
kout.lock();
kout.clear();
log.info() << "Allocating new string" << endl;
bse::string str1 = "This is a dynamically allocated string!";
kout << str1 << endl;
log.info() << "Reassign string" << endl;
str1 = "Hello";
kout << str1 << " has length " << str1.size() << endl;
kout << "Again with strlen: Hello has length " << bse::strlen("Hello") << endl;
kout << "Adding strings: " << str1 << " + World" << endl;
log.info() << "Adding strings" << endl;
str1 = str1 + " World";
kout << str1 << endl;
kout << "Hello += World" << endl;
log.info() << "Hello += World" << endl;
bse::string str3 = "Hello";
str3 += " World";
kout << str3 << endl;
kout << "String iterator!" << endl;
for (const char c : str1) {
kout << c << " ";
}
kout << endl;
log.info() << "Allocating new string" << endl;
bse::string str2 = "Hello World";
kout << "str1 == str2: " << static_cast<int>(str1 == str2) << endl;
kout << "strcmp(Hello, Hello): " << bse::strcmp("Hello", "Hello") << endl;
log.info() << "Reassign str2" << endl;
str2 = "Hello";
bse::array<char, 5> arr;
arr[0] = 'H';
arr[1] = 'e';
arr[2] = 'l';
arr[3] = 'l';
arr[4] = 'o';
kout << "bse::array<char, 5> to bse::string: " << arr << ", size: " << ((bse::string)arr).size() << endl;
kout << "(bse::string)arr (" << arr << ") == str2 (" << str2 << "): " << static_cast<int>((bse::string)arr == str2) << endl;
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,16 @@
#ifndef __StringDemo_include__
#define __StringDemo_include__
#include "kernel/Globals.h"
class StringDemo : public Thread {
private:
StringDemo(const StringDemo& copy) = delete;
public:
StringDemo() : Thread("StringDemo") {}
void run() override;
};
#endif