M*LIB: non-inlined strings, FuriString primitive (#1795)
* Quicksave 1 * Header stage complete * Source stage complete * Lint & merge fixes * Includes * Documentation step 1 * FBT: output free size considering BT STACK * Documentation step 2 * py lint * Fix music player plugin * unit test stage 1: string allocator, mem, getters, setters, appends, compare, search. * unit test: string equality * unit test: string replace * unit test: string start_with, end_with * unit test: string trim * unit test: utf-8 * Rename * Revert fw_size changes * Simplify CLI backspace handling * Simplify CLI character insert * Merge fixes * Furi: correct filenaming and spelling * Bt: remove furi string include Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -173,17 +173,20 @@ static bool file_stream_delete_and_insert(
|
||||
Stream* scratch_stream = file_stream_alloc(_stream->storage);
|
||||
|
||||
// TODO: we need something like "storage_open_tmpfile and storage_close_tmpfile"
|
||||
string_t scratch_name;
|
||||
string_t tmp_name;
|
||||
string_init(tmp_name);
|
||||
FuriString* scratch_name;
|
||||
FuriString* tmp_name;
|
||||
tmp_name = furi_string_alloc();
|
||||
storage_get_next_filename(
|
||||
_stream->storage, STORAGE_ANY_PATH_PREFIX, ".scratch", ".pad", tmp_name, 255);
|
||||
string_init_printf(scratch_name, ANY_PATH("%s.pad"), string_get_cstr(tmp_name));
|
||||
string_clear(tmp_name);
|
||||
scratch_name = furi_string_alloc_printf(ANY_PATH("%s.pad"), furi_string_get_cstr(tmp_name));
|
||||
furi_string_free(tmp_name);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(
|
||||
scratch_stream, string_get_cstr(scratch_name), FSAM_READ_WRITE, FSOM_CREATE_NEW))
|
||||
scratch_stream,
|
||||
furi_string_get_cstr(scratch_name),
|
||||
FSAM_READ_WRITE,
|
||||
FSOM_CREATE_NEW))
|
||||
break;
|
||||
|
||||
size_t current_position = stream_tell(stream);
|
||||
@@ -225,8 +228,8 @@ static bool file_stream_delete_and_insert(
|
||||
} while(false);
|
||||
|
||||
stream_free(scratch_stream);
|
||||
storage_common_remove(_stream->storage, string_get_cstr(scratch_name));
|
||||
string_clear(scratch_name);
|
||||
storage_common_remove(_stream->storage, furi_string_get_cstr(scratch_name));
|
||||
furi_string_free(scratch_name);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -67,8 +67,8 @@ static bool stream_write_struct(Stream* stream, const void* context) {
|
||||
return (stream_write(stream, write_data->data, write_data->size) == write_data->size);
|
||||
}
|
||||
|
||||
bool stream_read_line(Stream* stream, string_t str_result) {
|
||||
string_reset(str_result);
|
||||
bool stream_read_line(Stream* stream, FuriString* str_result) {
|
||||
furi_string_reset(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
@@ -84,13 +84,13 @@ bool stream_read_line(Stream* stream, string_t str_result) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
string_push_back(str_result, buffer[i]);
|
||||
furi_string_push_back(str_result, buffer[i]);
|
||||
result = true;
|
||||
break;
|
||||
} else if(buffer[i] == '\r') {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
furi_string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ bool stream_read_line(Stream* stream, string_t str_result) {
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
return furi_string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
bool stream_rewind(Stream* stream) {
|
||||
@@ -112,9 +112,10 @@ size_t stream_write_char(Stream* stream, char c) {
|
||||
return stream_write(stream, (const uint8_t*)&c, 1);
|
||||
}
|
||||
|
||||
size_t stream_write_string(Stream* stream, string_t string) {
|
||||
size_t stream_write_string(Stream* stream, FuriString* string) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)string_get_cstr(string), string_size(string));
|
||||
return stream_write(
|
||||
stream, (const uint8_t*)furi_string_get_cstr(string), furi_string_size(string));
|
||||
}
|
||||
|
||||
size_t stream_write_cstring(Stream* stream, const char* string) {
|
||||
@@ -134,10 +135,10 @@ size_t stream_write_format(Stream* stream, const char* format, ...) {
|
||||
|
||||
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
FuriString* data;
|
||||
data = furi_string_alloc_vprintf(format, args);
|
||||
size_t size = stream_write_string(stream, data);
|
||||
string_clear(data);
|
||||
furi_string_free(data);
|
||||
|
||||
return size;
|
||||
}
|
||||
@@ -153,7 +154,7 @@ bool stream_insert_char(Stream* stream, char c) {
|
||||
return stream_delete_and_insert_char(stream, 0, c);
|
||||
}
|
||||
|
||||
bool stream_insert_string(Stream* stream, string_t string) {
|
||||
bool stream_insert_string(Stream* stream, FuriString* string) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_string(stream, 0, string);
|
||||
}
|
||||
@@ -184,10 +185,10 @@ bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c) {
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string) {
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, FuriString* string) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(string), .size = string_size(string)};
|
||||
.data = (uint8_t*)furi_string_get_cstr(string), .size = furi_string_size(string)};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
@@ -213,12 +214,12 @@ bool stream_delete_and_insert_vaformat(
|
||||
const char* format,
|
||||
va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
FuriString* data;
|
||||
data = furi_string_alloc_vprintf(format, args);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(data), .size = string_size(data)};
|
||||
.data = (uint8_t*)furi_string_get_cstr(data), .size = furi_string_size(data)};
|
||||
bool result = stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
string_clear(data);
|
||||
furi_string_free(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -105,7 +104,7 @@ bool stream_delete_and_insert(
|
||||
* @return true if line lenght is not zero
|
||||
* @return false otherwise
|
||||
*/
|
||||
bool stream_read_line(Stream* stream, string_t str_result);
|
||||
bool stream_read_line(Stream* stream, FuriString* str_result);
|
||||
|
||||
/**
|
||||
* Moves the rw pointer to the start
|
||||
@@ -127,7 +126,7 @@ size_t stream_write_char(Stream* stream, char c);
|
||||
* @param string string value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_string(Stream* stream, string_t string);
|
||||
size_t stream_write_string(Stream* stream, FuriString* string);
|
||||
|
||||
/**
|
||||
* Write const char* to the stream
|
||||
@@ -182,7 +181,7 @@ bool stream_insert_char(Stream* stream, char c);
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_string(Stream* stream, string_t string);
|
||||
bool stream_insert_string(Stream* stream, FuriString* string);
|
||||
|
||||
/**
|
||||
* Insert const char* to the stream
|
||||
@@ -231,7 +230,7 @@ bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string);
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, FuriString* string);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert const char* to the stream
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
typedef struct {
|
||||
Stream stream_base;
|
||||
string_t string;
|
||||
FuriString* string;
|
||||
size_t index;
|
||||
} StringStream;
|
||||
|
||||
@@ -39,14 +39,14 @@ const StreamVTable string_stream_vtable = {
|
||||
|
||||
Stream* string_stream_alloc() {
|
||||
StringStream* stream = malloc(sizeof(StringStream));
|
||||
string_init(stream->string);
|
||||
stream->string = furi_string_alloc();
|
||||
stream->index = 0;
|
||||
stream->stream_base.vtable = &string_stream_vtable;
|
||||
return (Stream*)stream;
|
||||
}
|
||||
|
||||
static void string_stream_free(StringStream* stream) {
|
||||
string_clear(stream->string);
|
||||
furi_string_free(stream->string);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ static bool string_stream_eof(StringStream* stream) {
|
||||
|
||||
static void string_stream_clean(StringStream* stream) {
|
||||
stream->index = 0;
|
||||
string_reset(stream->string);
|
||||
furi_string_reset(stream->string);
|
||||
}
|
||||
|
||||
static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
@@ -79,8 +79,8 @@ static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffse
|
||||
}
|
||||
break;
|
||||
case StreamOffsetFromEnd:
|
||||
if(((int32_t)string_size(stream->string) + offset) >= 0) {
|
||||
stream->index = string_size(stream->string) + offset;
|
||||
if(((int32_t)furi_string_size(stream->string) + offset) >= 0) {
|
||||
stream->index = furi_string_size(stream->string) + offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
@@ -88,7 +88,7 @@ static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffse
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t diff = (stream->index - string_size(stream->string));
|
||||
int32_t diff = (stream->index - furi_string_size(stream->string));
|
||||
if(diff > 0) {
|
||||
stream->index -= diff;
|
||||
result = false;
|
||||
@@ -102,7 +102,7 @@ static size_t string_stream_tell(StringStream* stream) {
|
||||
}
|
||||
|
||||
static size_t string_stream_size(StringStream* stream) {
|
||||
return string_size(stream->string);
|
||||
return furi_string_size(stream->string);
|
||||
}
|
||||
|
||||
static size_t string_stream_write(StringStream* stream, const char* data, size_t size) {
|
||||
@@ -117,7 +117,7 @@ static size_t string_stream_write(StringStream* stream, const char* data, size_t
|
||||
|
||||
static size_t string_stream_read(StringStream* stream, char* data, size_t size) {
|
||||
size_t write_index = 0;
|
||||
const char* cstr = string_get_cstr(stream->string);
|
||||
const char* cstr = furi_string_get_cstr(stream->string);
|
||||
|
||||
if(!string_stream_eof(stream)) {
|
||||
while(true) {
|
||||
@@ -145,17 +145,17 @@ static bool string_stream_delete_and_insert(
|
||||
remain_size = MIN(delete_size, remain_size);
|
||||
|
||||
if(remain_size != 0) {
|
||||
string_replace_at(stream->string, stream->index, remain_size, "");
|
||||
furi_string_replace_at(stream->string, stream->index, remain_size, "");
|
||||
}
|
||||
}
|
||||
|
||||
if(write_callback) {
|
||||
string_t right;
|
||||
string_init_set(right, &string_get_cstr(stream->string)[stream->index]);
|
||||
string_left(stream->string, string_stream_tell(stream));
|
||||
FuriString* right;
|
||||
right = furi_string_alloc_set(&furi_string_get_cstr(stream->string)[stream->index]);
|
||||
furi_string_left(stream->string, string_stream_tell(stream));
|
||||
result &= write_callback((Stream*)stream, ctx);
|
||||
string_cat(stream->string, right);
|
||||
string_clear(right);
|
||||
furi_string_cat(stream->string, right);
|
||||
furi_string_free(right);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -169,9 +169,9 @@ static bool string_stream_delete_and_insert(
|
||||
*/
|
||||
static size_t string_stream_write_char(StringStream* stream, char c) {
|
||||
if(string_stream_eof(stream)) {
|
||||
string_push_back(stream->string, c);
|
||||
furi_string_push_back(stream->string, c);
|
||||
} else {
|
||||
string_set_char(stream->string, stream->index, c);
|
||||
furi_string_set_char(stream->string, stream->index, c);
|
||||
}
|
||||
stream->index++;
|
||||
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
Reference in New Issue
Block a user