ppdfviewer, basic, bits and string view
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#include "stringv.h"
|
||||
#include <stdio.h>
|
||||
int main(void) {
|
||||
StringView s1 = sv_from_cstr("Hola mundo con string view");
|
||||
StringView s2 = sv_from_cstr("😊");
|
||||
StringView last = sv_right(&s1, 4);
|
||||
|
||||
printf("El string view contiene: %s\n", s2.text);
|
||||
printf("El length es: %zu\n", s2.lenght);
|
||||
|
||||
printf("last tiene:%.*s\n", (int)last.lenght, last.text);
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,60 @@
|
||||
#include "stringv.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* @brief Create a string view from a const char* and set their lenght
|
||||
*
|
||||
* @details This string view provides a view of a string. It does not allocate
|
||||
* memory, therefore, it lives only while the source is alive. The
|
||||
* lenght is a bytes number of the string. It does not represents the number of
|
||||
* characters.
|
||||
*
|
||||
* @param str pointer from the original string. It can be NULL.
|
||||
*
|
||||
* @return a StringView with the view of original string with corresponding
|
||||
* lenght.
|
||||
*/
|
||||
|
||||
#define STRING_VIEW_NULL (StringView){.text = NULL, .lenght = 0}
|
||||
|
||||
StringView sv_from_cstr(const char *str) {
|
||||
if (str == NULL) {
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
return (StringView){.text = (char *)str, .lenght = strlen(str)};
|
||||
}
|
||||
|
||||
StringView sv_left(const StringView *sv, size_t l) {
|
||||
if (sv == NULL) {
|
||||
perror("sv_left: pointer is NULL\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
if (sv->text == NULL) {
|
||||
perror("sv_left: text pointer is NULL\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
if (sv->lenght < l) {
|
||||
perror("sv_left: text length is less than specified length\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
return (StringView){.text = sv->text, .lenght = l};
|
||||
}
|
||||
|
||||
StringView sv_right(const StringView *sv, size_t r) {
|
||||
if (sv == NULL) {
|
||||
perror("sv_right: pointer is NULL\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
if (sv->text == NULL) {
|
||||
perror("sv_right: text pointer is NULL\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
if (sv->lenght < r) {
|
||||
perror("sv_right: text length is less than specified length\n");
|
||||
return STRING_VIEW_NULL;
|
||||
}
|
||||
size_t nlength = sv->lenght - r;
|
||||
return (StringView){.text = sv->text + nlength, .lenght = r};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef STRING_VIEW_H
|
||||
#define STRING_VIEW_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct StringView {
|
||||
char *text;
|
||||
size_t lenght;
|
||||
} StringView;
|
||||
|
||||
StringView sv_from_cstr(const char *);
|
||||
StringView sv_left(const StringView *sv, size_t l);
|
||||
StringView sv_right(const StringView *sv, size_t l);
|
||||
#endif
|
||||
Reference in New Issue
Block a user