Похожие чаты

@TRGWII I updated my words finder to use the new

style. I don't know how to write it without using sprintf and fwrite, and I figured memcpy was harmless, so those are still in there. Do you think this is better? Do you code without using sprintf?
#include <stdlib.h>
#include <stdio.h>
#include "app/src/chibits.h" // for s8 and typedefs, etc.
#include <string.h> // for memcpy


typedef struct {
size beginI, endI;
} Region;


// Assumes UTF-8 or ISO-8859-1 (latin)
static inline bool isGraphChr(u8 ro chr) {
return (chr >= '!' && chr <= '~') || chr > 0xA0;
}


#define s8alloc(size) \
(s8) { (u8[size]) { }, 0 }


static inline void s8write(s8 ro s, FILE * ro stream) {
fwrite(s.data, 1, s.len, stream);
}


static inline void s8sub(
s8 * ro dest,
s8 ro src,
size ro from,
size ro len) {
memcpy(dest->data + dest->len, src.data + from, len);
dest->len += len;
}


int main(void) {

// Example data
s8 ro str = s8(
"Once upon a time in a land far far away there was a strange and vast forest");

// Storage
i8 nWords = 0;
size ro nWordRegions = 30;
Region wordRegions[nWordRegions];

// Find the words
{
bool inWord = false;
size i = 0;
for (; i < str.len; i++) {
if (inWord) {
if (isGraphChr(str.data[i])) { continue; }
inWord = false;
wordRegions[nWords - 1].endI = i;
continue;
}
if (isGraphChr(str.data[i])) {
if (nWords > nWordRegions) {
fprintf(stderr, "Reached word region storage limit");
return EXIT_FAILURE;
}
inWord = true;
wordRegions[nWords++].beginI = i;
continue;
}
}
if (inWord) { wordRegions[nWords - 1].endI = i; }
}

// Format output
s8 outp = s8alloc(2000); // this number here is just a complete guess
for (i8 i = 0; i < nWords; i++) {
Region ro wr = wordRegions[i];
size ro wordLen = wr.endI - wr.beginI;
int lnLen; // has to be int otherwise warnings
sprintf(
outp.data + outp.len,
"%" PRIi8 ": From %ld to %ld (length %ld): %n",
i,
wr.beginI,
wr.endI,
wordLen,
&lnLen);
outp.len += lnLen;
s8sub(&outp, str, wr.beginI, wordLen);
outp.data[outp.len++] = '\n';
}

s8write(outp, stdout);
return EXIT_SUCCESS;
}

1 ответов

18 просмотров

looks slightly better to me, although I'm not so sure how sound s8alloc is

Похожие вопросы

Обсуждают сегодня

а зачем этот вопрос для удаления из чата?
Mёdkinson Medvezhkin
63
using next image component with s3 image: "url" parameter is valid but upstream response is invalid code: <Image fill src={s...
Fasil
3
Эх кто-то пришел и весь праздник испортил :( You need complex FBX scene importing setup to change things on import? good luck with that. You need navigation and pathfinding? g...
Serg Gini
5
Friends, how can I find my Wazirx wallet address?
Silm Silm
31
Всем привет! Подскажите. Я написал приложение на Delphi 10.2 Tokyo под Windows 10. И передо мной стал вопрос о том чтобы сделать это приложение кроссплатформенным (под Linux и...
Дмитрий Завгородний
24
Какого хера? /Sources/App/Modules/User/Models/UserLinkApple.swift:21:20: warning: stored property '_id' of 'Sendable'-conforming class 'UserLinkApple' is mutable @ID(...
Alexander Sherbakov
12
My 7 year old daughter recognizes the Bitcoin symbol and knows it's currency. What are the top ,3-5 most basic important concepts to convey to a kid about BCH? I started with ...
Big Hair
24
How many Cashfusion server now?
Crypto life
22
But I'm wondering how exactly we can increase our chances of winning. Do you think it's just a matter of luck, or are there specific actions we need to take to qualify?
Luqman Ayman
12
Кто знает локации, где можно машину красиво отфоткать?
Lalalashechki Lalala
23
Карта сайта