Thread View: pl.comp.lang.c
4 messages
4 total messages
Started by (fir)
Thu, 14 Sep 2017 21:07
chunk2int (or str2int) how it SHOULD work?
Author: (fir)
Date: Thu, 14 Sep 2017 21:07
Date: Thu, 14 Sep 2017 21:07
61 lines
4531 bytes
4531 bytes
i need to define most general purspose function
that i will be using in various codes just to
extract integers form various texts
if given word is just pure integer like "123" "-300"
it is strightforward, but what if such word is a nomber which is somewhat polluted like "(123)" or "(123" "^123"
"123v" "12 3" "a123" and so on..? what is most logical and most wiidespread to use?
right now i got two functions, one is a wrapper on strtol
so it worx like strtol second is also wrapper on strtol
but as strtol fails on letters preceeding numbers i wrote one that skips those letters and then calls strtol
(they both also give output info if no number was extracted or out of range errr do appear)
those are example results of their work
a NIL NIL
b NIL NIL
c NIL NIL
d NIL NIL
1 1 1
2 2 2
3 3 3
4 4 4
a1 NIL 1
a2 NIL 2
a3 NIL 3
3d 3 3
3h 3 3
0xff 255 255
0xffff 65535 65535
0x NIL NIL
0b111001 0 0
1e4 1 1
-0x45 -69 -69
-2 -2 -2
a- NIL NIL
3- 3 3
4- 4 4
--5 NIL -5
- NIL NIL
6 6 6
- NIL NIL
a2a NIL 2
a-22-3 NIL -22
3f4f 3 3
(2) NIL 2
+6 6 6
)89 NIL 89
=78 NIL 78
*4 NIL 4
^4 NIL 4
7123456789 RANGE RANGE
0x223344556 RANGE RANGE
8 8 8
9 9 9
0.23 0 0
.84 NIL 84
.12. NIL 12
.23 NIL 23
im not sure if i would in practice needed more first or more second or maybe both, or maybe i should define such functions in some way other - but how?
Re: chunk2int (or str2int) how it SHOULD work?
Author: dawid.bautsch
Date: Fri, 15 Sep 2017 12:20
Date: Fri, 15 Sep 2017 12:20
13 lines
2247 bytes
2247 bytes
My proposition is to split the whole process into three stages. Stage 1: The input string has to be split by lines. The function will try to separate each number. Here you must make an assumption: at least two spaces (or even one if you wish, or tab character) are used to separate number. Stage 2: input data normalization. Removing non numeric data, white spaces and so on from each extracted element. There are some corner case here: * if the input string contains "0x" symbol, then we treat it like a hex string literal. * if the input string begins with "." or "," then we assume it is a float number. Stage 3: Converting ascii (or unicode) symbols into number. PS> Example trash input data : "a-22-3" This is an invalid number. It could be read as "22" or "223". Both of them are invalid i suspose. So it should be discarded during first stage.
Re: chunk2int (or str2int) how it SHOULD work?
Author: (fir)
Date: Fri, 15 Sep 2017 14:52
Date: Fri, 15 Sep 2017 14:52
204 lines
8235 bytes
8235 bytes
W dniu piątek, 15 września 2017 14:20:31 UTC+2 użytkownik dawid....@gmail.com napisał:
> My proposition is to split the whole process into three stages.
>
> Stage 1: The input string has to be split by lines. The function will try to separate each number. Here you must make an assumption: at least two spaces (or even one if you wish, or tab character) are used to separate number.
>
>
> Stage 2: input data normalization. Removing non numeric data, white spaces and so on from each extracted element.
> There are some corner case here:
> * if the input string contains "0x" symbol, then we treat it like a hex string literal.
> * if the input string begins with "." or "," then we assume it is a float number.
>
> Stage 3: Converting ascii (or unicode) symbols into number.
>
> PS> Example trash input data : "a-22-3" This is an invalid number. It could be read as "22" or "223". Both of them are invalid i suspose. So it should be discarded during first stage.
podzial na slowa nie jest w gestii tego pytania, mozna zalozyc ze juz masz jakis podany string
z ktiorego chcesz pozyskac liczbe, pytanie jak
to wtedy zaprojektowac by bylo najbardziej uzyteczne
(najbardziej ogolnie uzyteczne) jako funkcja biblioteczna
na razie zrobilem to tak
" (...) here above it uses globals to transfer additional info, i was thinking on it and decided tht i will better pass the pointer to internal static data
(or pass structure by value as i need it seems only
5 additional bits
so (revritten version thogh still ugly (it is not edited to look good - just tests)
struct C2I
{
int value;
int failed :1 ;
int more_numbers :1 ;
int polluted_left :1 ;
int polluted_right : 1 ;
int out_of_range :1;
};
C2I Chunk2Int(chunk word)
{
static C2I c2i = {0};
c2i.value = 0;
c2i.failed = 0;
c2i.more_numbers = 0;
c2i.polluted_left = 0;
c2i.polluted_right = 0;
c2i.out_of_range = 0;
// char* str = CopyChunkToStrZ_Static(word);
char* q = word.beg;
int left_dirt = SkipCharsTillNumber_Limited(&q, word.end);
if(left_dirt>0) c2i.polluted_left = 1;
if(q>word.end)
{
c2i.failed = 1;
c2i.polluted_left = 0;
return c2i;
}
static char* p = 0;
errno =0;
int value = (int) strtol(q, &p, 0);
c2i.value = value;
if(p-q==0)
{
c2i.failed = 1;
return c2i;
}
if(errno)
{
c2i.failed = 1;
c2i.out_of_range = 1;
return c2i;
}
if(1)
if(p<=word.end)
{
q=p;
int right_dirt = SkipCharsTillNumber_Limited(&q, word.end);
if(right_dirt>0) c2i.polluted_right = 1;
if(q>word.end)
return c2i;
// int value2 = (int) strtol(q, &p, 0);
// if(p-q==0) return value;
c2i.more_numbers = 1;
}
return c2i;
}
such code using my sicl routines (which this Chunk2Int() is meant to be a part of it)
char* txt = "a b c d 1 2 3 4 a1 a2 a3 3d 3h 0xff 0xffff 0x 0b111001 1e4 -0x45 -2 a- 3- 4- --5 - 6 - a2a a-22-3 3f4f (2) +6 )89 =78 *4 ^4 7123456789 0x223344556 8 9 0.23 .84 .12. .23";
int main(int argc, char* argv[])
{
chunks words = SplitAllStdWords( Chunk(txt) );
int len = ChunksLength(words);
for(int i=0; i<len; i++)
{
printf("\n ");
PrintChunkTab16(words.first[i]);
C2I res = Chunk2Int(words.first[i]);
if(!res.failed) printf(" %15d ", res.value);
if(res.failed) printf(" %15s ", "nil");
if(res.out_of_range) printf(" out of range ");
if(res.more_numbers) printf(" more numbers ");
if(res.polluted_left) printf(" polluted left ");
if(res.polluted_right) printf(" polluted right ");
}
return 0;
}
gives such output
a nil
b nil
c nil
d nil
1 1
2 2
3 3
4 4
a1 1 polluted left
a2 2 polluted left
a3 3 polluted left
3d 3 polluted right
3h 3 polluted right
0xff 255
0xffff 65535
0x nil
0b111001 0 more numbers polluted right
1e4 1 more numbers polluted right
-0x45 -69
-2 -2
a- nil
3- 3 polluted right
4- 4 polluted right
--5 -5 polluted left
- nil
6 6
- nil
a2a 2 polluted left polluted right
a-22-3 -22 more numbers polluted left
3f4f 3 more numbers polluted right
(2) 2 polluted left polluted right
+6 6 polluted left
)89 89 polluted left
=78 78 polluted left
*4 4 polluted left
^4 4 polluted left
7123456789 nil out of range
0x223344556 nil out of range
8 8
9 9
0.23 0 more numbers polluted right
.84 84 polluted left
.12. 12 polluted left polluted right
.23 23 polluted left
seems to be ok, but still searching for improvements
some ideas of improvementing (improoving) this? "
to wyglada jako tako ok ale faktycznie jest problem z kwestią floatow, chyba faktycznie byloby lepiej gdyby
takie kawalki jak "1.23e6" 12.34" 'parsowalo' od razu jako floata i jedynie przed zwroceniem wyniku rzutowalo na inta ustawiajac ew flage .zaszlo_rzutowanie_z_floata (ew jest tez opcja moze zostawienie tego i dorobienia drugiej funkcji Chunk2Double, ktos wie moze czy da sie to latwo przerobic na wersje doublową (nie googlowalem jeszcze a z glowy nie pamietam czym sie wczytywalo double))
Re: chunk2int (or str2int) how it SHOULD work?
Author: (fir)
Date: Fri, 15 Sep 2017 18:32
Date: Fri, 15 Sep 2017 18:32
52 lines
4649 bytes
4649 bytes
W dniu piątek, 15 września 2017 16:52:03 UTC+2 użytkownik (fir) napisał:
>
> to wyglada jako tako ok ale faktycznie jest problem z kwestią floatow, chyba faktycznie byloby lepiej gdyby
> takie kawalki jak "1.23e6" 12.34" 'parsowalo' od razu jako floata i jedynie przed zwroceniem wyniku rzutowalo na inta ustawiajac ew flage .zaszlo_rzutowanie_z_floata (ew jest tez opcja moze zostawienie tego i dorobienia drugiej funkcji Chunk2Double, ktos wie moze czy da sie to latwo przerobic na wersje doublową (nie googlowalem jeszcze a z glowy nie pamietam czym sie wczytywalo double))
funkcje dla daubli da sie zrobic niemal bez zmian wzgledem powyzszego na strtod (trzeba tylko w tej funkcji do pomijania junkow zrobic by nie pomijala kropki jesli jest nastąpiana digitem)
jednakowoz ta funkcja da dabli lyka dable ale wysypuje sie na hexach (0x-ach)
1 1.000000
2 2.000000
3 3.000000
4 4.000000
a1 1.000000 polluted left
a2 2.000000 polluted left
a3 3.000000 polluted left
3d 3.000000 polluted right
3h 3.000000 polluted right
0xff 0.000000 polluted right
0xffff 0.000000 polluted right
0x 0.000000 polluted right
0b111001 0.000000 more numbers polluted right
1e4 10000.000000
-0x45 -0.000000 more numbers polluted right
-2 -2.000000
a- nil
3- 3.000000 polluted right
4- 4.000000 polluted right
--5 -5.000000 polluted left
- nil
6 6.000000
- nil
a2a 2.000000 polluted left polluted right
a-22-3 -22.000000 more numbers polluted left
3f4f 3.000000 more numbers polluted right
(2) 2.000000 polluted left polluted right
+6 6.000000 polluted left
)89 89.000000 polluted left
=78 78.000000 polluted left
*4 4.000000 polluted left
^4 4.000000 polluted left
7123456789 7123456789.000000
0x223344556 0.000000 more numbers polluted right
8 8.000000
9 9.000000
0.23 0.230000
.84 0.840000
.12. 0.120000 polluted right
.23 0.230000
nie wiem czy w obliczu tej sytuacji jak przedstawiona wchodzic dalej w ten temat czy nie warto - jako ze te dwie funkcje nie udostepniaja idealnej funkcjonalnosci ale udostepniaja tez jednak pewną funkcjonalnosc w wiekszosci wypadkow wystarczajacą
(sporo lepsza niz czyste atoi/atof strtol/strtod)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads