Article View: pl.comp.lang.c
Article #309447Re: chunk2int (or str2int) how it SHOULD work?
From: (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))
Technical Details
Message-ID:
<e8a3e9ca-4ad0-4afa-aad8-9cb5e69f2a66@googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!b1no781263qtc.1!news-out.google.com!x15ni270qth.1!nntp.google.com!b1no781259qtc.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailReferences:
<f762205f-d614-4267-813a-f181066a6ae2@googlegroups.com> <d8c69713-0a0f-4664-8349-1ddd667e401f@googlegroups.com>Show Full Headers
Complete Article Headers
X-Received: by 10.13.235.131 with SMTP id u125mr16335770ywe.0.1505487121855; Fri, 15 Sep 2017 07:52:01 -0700 (PDT) X-Received: by 10.31.152.136 with SMTP id a130mr286286vke.15.1505487121756; Fri, 15 Sep 2017 07:52:01 -0700 (PDT) Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!news.unit0.net!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!b1no781263qtc.1!news-out.google.com!x15ni270qth.1!nntp.google.com!b1no781259qtc.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: pl.comp.lang.c Date: Fri, 15 Sep 2017 07:52:01 -0700 (PDT) In-Reply-To: <d8c69713-0a0f-4664-8349-1ddd667e401f@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=5.172.255.127; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN NNTP-Posting-Host: 5.172.255.127 References: <f762205f-d614-4267-813a-f181066a6ae2@googlegroups.com> <d8c69713-0a0f-4664-8349-1ddd667e401f@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <e8a3e9ca-4ad0-4afa-aad8-9cb5e69f2a66@googlegroups.com> Subject: Re: chunk2int (or str2int) how it SHOULD work? From: "(fir)" <profesor.fir@gmail.com> Injection-Date: Fri, 15 Sep 2017 14:52:01 +0000 Lines: 227 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit