Compare commits

...

5 Commits

Author SHA1 Message Date
Bananymous 1ac7629459 BAN: Implement StringView::rfind() 2024-06-25 11:04:03 +03:00
Bananymous 95681a7a05 LibImage: Cleanup bicubic calculations 2024-06-25 11:04:03 +03:00
Bananymous d7b8458a56 Kernel: Fix TCP sending
TCP send was effectively always waiting for connection to close and then
return a value of 0.
2024-06-25 11:04:03 +03:00
Bananymous 67dfe0bcf3 BAN: Allow String::formatted to fail 2024-06-25 11:04:03 +03:00
Bananymous b1869bced4 BAN: Implement StringView::starts_with() 2024-06-25 11:04:03 +03:00
13 changed files with 58 additions and 24 deletions

View File

@ -26,10 +26,15 @@ namespace BAN
~String() { clear(); } ~String() { clear(); }
template<typename... Args> template<typename... Args>
static String formatted(const char* format, const Args&... args) static BAN::ErrorOr<String> formatted(const char* format, Args&&... args)
{ {
size_type length = 0;
BAN::Formatter::print([&](char) { length++; }, format, BAN::forward<Args>(args)...);
String result; String result;
BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, args...); TRY(result.reserve(length));
BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, BAN::forward<Args>(args)...);
return result; return result;
} }

View File

@ -165,6 +165,37 @@ namespace BAN
return {}; return {};
} }
BAN::Optional<size_type> rfind(char ch) const
{
for (size_type i = m_size; i > 0; i--)
if (m_data[i - 1] == ch)
return i - 1;
return {};
}
BAN::Optional<size_type> rfind(bool(*comp)(char)) const
{
for (size_type i = m_size; i > 0; i--)
if (comp(m_data[i - 1]))
return i - 1;
return {};
}
constexpr bool starts_with(BAN::StringView target) const
{
if (target.size() > m_size)
return false;
for (size_type i = 0; i < m_size - target.size(); i++)
{
bool valid = true;
for (size_type j = 0; j < target.size() && valid; j++)
valid = (m_data[i + j] == target[j]);
if (valid)
return true;
}
return false;
}
constexpr bool contains(char ch) const constexpr bool contains(char ch) const
{ {
for (size_type i = 0; i < m_size; i++) for (size_type i = 0; i < m_size; i++)

View File

@ -41,7 +41,7 @@ namespace Kernel
FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp) FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp)
: CharacterDevice(mode, uid, gid) : CharacterDevice(mode, uid, gid)
, m_name(BAN::String::formatted("fb{}", minor(rdev))) , m_name(MUST(BAN::String::formatted("fb{}", minor(rdev))))
, m_rdev(rdev) , m_rdev(rdev)
, m_video_memory_paddr(paddr) , m_video_memory_paddr(paddr)
, m_width(width) , m_width(width)

View File

@ -29,7 +29,7 @@ namespace Kernel
BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process) BAN::ErrorOr<void> ProcFileSystem::on_process_create(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = TRY(BAN::String::formatted("{}", process.pid()));
auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid())); auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid()));
TRY(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path)); TRY(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
return {}; return {};
@ -37,7 +37,7 @@ namespace Kernel
void ProcFileSystem::on_process_delete(Process& process) void ProcFileSystem::on_process_delete(Process& process)
{ {
auto path = BAN::String::formatted("{}", process.pid()); auto path = MUST(BAN::String::formatted("{}", process.pid()));
auto inode = MUST(root_inode()->find_inode(path)); auto inode = MUST(root_inode()->find_inode(path));
static_cast<ProcPidInode*>(inode.ptr())->cleanup(); static_cast<ProcPidInode*>(inode.ptr())->cleanup();

View File

@ -12,7 +12,7 @@ namespace Kernel::Input
PS2Device::PS2Device(PS2Controller& controller) PS2Device::PS2Device(PS2Controller& controller)
: CharacterDevice(0440, 0, 901) : CharacterDevice(0440, 0, 901)
, m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device())) , m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device()))
, m_name(BAN::String::formatted("input{}", minor(m_rdev))) , m_name(MUST(BAN::String::formatted("input{}", minor(m_rdev))))
, m_controller(controller) , m_controller(controller)
{ } { }

View File

@ -259,7 +259,7 @@ namespace Kernel
const uint32_t target_ack = m_send_window.start_seq + m_send_window.data_size; const uint32_t target_ack = m_send_window.start_seq + m_send_window.data_size;
m_semaphore.unblock(); m_semaphore.unblock();
while (m_send_window.start_seq < target_ack) while (m_send_window.current_ack < target_ack)
{ {
if (m_state != State::Established) if (m_state != State::Established)
return return_with_maybe_zero(); return return_with_maybe_zero();

View File

@ -21,7 +21,7 @@ namespace Kernel
, m_first_block(first_block) , m_first_block(first_block)
, m_last_block(last_block) , m_last_block(last_block)
, m_attributes(attr) , m_attributes(attr)
, m_name(BAN::String::formatted("{}{}", name_prefix, index)) , m_name(MUST(BAN::String::formatted("{}{}", name_prefix, index)))
, m_rdev(makedev(major(device->rdev()), index)) , m_rdev(makedev(major(device->rdev()), index))
{ {
memcpy(m_label, label, sizeof(m_label)); memcpy(m_label, label, sizeof(m_label));

View File

@ -179,11 +179,10 @@ namespace Kernel
SerialTTY::SerialTTY(Serial serial) SerialTTY::SerialTTY(Serial serial)
: TTY(0600, 0, 0) : TTY(0600, 0, 0)
, m_name(MUST(BAN::String::formatted("ttyS{}", minor(rdev()))))
, m_serial(serial) , m_serial(serial)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {}
m_name = BAN::String::formatted("ttyS{}", minor(rdev()));
}
BAN::ErrorOr<BAN::RefPtr<SerialTTY>> SerialTTY::create(Serial serial) BAN::ErrorOr<BAN::RefPtr<SerialTTY>> SerialTTY::create(Serial serial)
{ {

View File

@ -43,11 +43,10 @@ namespace Kernel
VirtualTTY::VirtualTTY(TerminalDriver* driver) VirtualTTY::VirtualTTY(TerminalDriver* driver)
: TTY(0600, 0, 0) : TTY(0600, 0, 0)
, m_name(MUST(BAN::String::formatted("tty{}", minor(rdev()))))
, m_terminal_driver(driver) , m_terminal_driver(driver)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {
m_name = BAN::String::formatted("tty{}", minor(rdev()));
m_width = m_terminal_driver->width(); m_width = m_terminal_driver->width();
m_height = m_terminal_driver->height(); m_height = m_terminal_driver->height();

View File

@ -41,7 +41,7 @@ BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
if (command[i] == '?') if (command[i] == '?')
{ {
i++; i++;
return BAN::String::formatted("{}", last_return); return MUST(BAN::String::formatted("{}", last_return));
} }
if (isalnum(command[i])) if (isalnum(command[i]))
{ {

View File

@ -52,7 +52,7 @@ Config parse_config()
return config; return config;
} }
auto config_path = BAN::String::formatted("{}/.config/WindowServer.conf", home_env); auto config_path = MUST(BAN::String::formatted("{}/.config/WindowServer.conf", home_env));
FILE* fconfig = fopen(config_path.data(), "r"); FILE* fconfig = fopen(config_path.data(), "r");
if (!fconfig) if (!fconfig)
{ {

View File

@ -163,7 +163,7 @@ namespace LibImage
const auto b = p[0] + (p[1] * -2.5) + (p[2] * 2.0) + (p[3] * -0.5); const auto b = p[0] + (p[1] * -2.5) + (p[2] * 2.0) + (p[3] * -0.5);
const auto c = (p[0] * -0.5) + (p[2] * 0.5); const auto c = (p[0] * -0.5) + (p[2] * 0.5);
const auto d = p[1]; const auto d = p[1];
return (a * x * x * x) + (b * x * x) + (c * x) + d; return ((a * x + b) * x + c) * x + d;
}; };
for (uint64_t y = 0; y < new_height; y++) for (uint64_t y = 0; y < new_height; y++)

View File

@ -72,14 +72,14 @@ BAN::String build_access_string(mode_t mode)
BAN::String build_hard_links_string(nlink_t links) BAN::String build_hard_links_string(nlink_t links)
{ {
return BAN::String::formatted("{}", links); return MUST(BAN::String::formatted("{}", links));
} }
BAN::String build_owner_name_string(uid_t uid) BAN::String build_owner_name_string(uid_t uid)
{ {
struct passwd* passwd = getpwuid(uid); struct passwd* passwd = getpwuid(uid);
if (passwd == nullptr) if (passwd == nullptr)
return BAN::String::formatted("{}", uid); return MUST(BAN::String::formatted("{}", uid));
return BAN::String(BAN::StringView(passwd->pw_name)); return BAN::String(BAN::StringView(passwd->pw_name));
} }
@ -87,13 +87,13 @@ BAN::String build_owner_group_string(gid_t gid)
{ {
struct group* grp = getgrgid(gid); struct group* grp = getgrgid(gid);
if (grp == nullptr) if (grp == nullptr)
return BAN::String::formatted("{}", gid); return MUST(BAN::String::formatted("{}", gid));
return BAN::String(BAN::StringView(grp->gr_name)); return BAN::String(BAN::StringView(grp->gr_name));
} }
BAN::String build_size_string(off_t size) BAN::String build_size_string(off_t size)
{ {
return BAN::String::formatted("{}", size); return MUST(BAN::String::formatted("{}", size));
} }
BAN::String build_month_string(BAN::Time time) BAN::String build_month_string(BAN::Time time)
@ -104,15 +104,15 @@ BAN::String build_month_string(BAN::Time time)
BAN::String build_day_string(BAN::Time time) BAN::String build_day_string(BAN::Time time)
{ {
return BAN::String::formatted("{}", time.day); return MUST(BAN::String::formatted("{}", time.day));
} }
BAN::String build_time_string(BAN::Time time) BAN::String build_time_string(BAN::Time time)
{ {
static uint32_t current_year = ({ timespec real_time; clock_gettime(CLOCK_REALTIME, &real_time); BAN::from_unix_time(real_time.tv_sec).year; }); static uint32_t current_year = ({ timespec real_time; clock_gettime(CLOCK_REALTIME, &real_time); BAN::from_unix_time(real_time.tv_sec).year; });
if (time.year != current_year) if (time.year != current_year)
return BAN::String::formatted("{}", time.year); return MUST(BAN::String::formatted("{}", time.year));
return BAN::String::formatted("{2}:{2}", time.hour, time.minute); return MUST(BAN::String::formatted("{2}:{2}", time.hour, time.minute));
} }
int list_directory(const BAN::String& path, config_t config) int list_directory(const BAN::String& path, config_t config)
@ -213,7 +213,7 @@ int list_directory(const BAN::String& path, config_t config)
GET_ENTRY_STRING(day, time); GET_ENTRY_STRING(day, time);
GET_ENTRY_STRING(time, time); GET_ENTRY_STRING(time, time);
full_entry.full_name = BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name); full_entry.full_name = MUST(BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name));
MUST(full_entries.push_back(BAN::move(full_entry))); MUST(full_entries.push_back(BAN::move(full_entry)));
} }