Commit c249e122 authored by Hans Buchmann's avatar Hans Buchmann

String::copy

String::cmp
String::to unsigned -> string
           string   -> unsigned
parent bf4b5787
......@@ -102,21 +102,17 @@ unsigned String::len(char* s)
|------------- terminating zero
dst->|a|b|c|d|e|f|g|h|j|j|\0|x|x|x|....
*/
void String::copy(char* dst,char* src)
//sizeof(dst)==8 byte (64 bit) dst is a pointer
//java: dst.length
{
//special cases
if ((src==0) || (dst==0)) return;
//|------------------------- logical or
if ((dst==0) || (src==0)) return;
//|--------------------------- logical or
unsigned i=0; //index in dst/src
while(true)
{
char ch=src[i];
dst[i]=ch; //copy character
auto ch=src[i];
dst[i]=ch;
if (ch=='\0') return;
i=i+1;
}
......@@ -136,41 +132,45 @@ int String::cmp(char* s1,char* s2)
auto ch2=s2[i];
if (ch1!=ch2)
{
return ch1-ch2; //
return ch1-ch2; //ascii code ch1 - ascii code ch2
}
//ch1==ch2
if (ch1=='\0') return 0;
i=i+1;
//ch1==ch2
if (ch1=='\0') return 0; //strings are equal
i=i+1;
}
}
void String::to(unsigned v,char s[])
{
// 01234567890
// 2^32= 4294967296 exercise how many decimal digits log(2^32)=32*log(2)=32*0.3
// 2^32= 4294967296 exercise how many decimal digits 1+log(2^32)=1+32*log(2)=1+32*0.3
//index 0123456789
static const char Digits[]="0123456789"; //array of char
//example v=123
/*
step vi vi/10 vi%10
-------------------
0 123 12 3
1 12 1 2
2 1 0 1
step vi vi/10 vi%10
-------------------------
0 12345 1234 5
1 1234 123 4
2 123 12 3
3 12 1 2
4 1 0 1
*/
char revdig[20];
unsigned i=0; //index in revdig
char revdig[20];
do
{
auto d=v%10; //0<=d<10: 0 1 2 3 4 5 6 7 8 9
revdig[i]=Digits[d];
i=i+1;
auto d=v%10; //0<=d<9: 0 1 2 3 4 5 6 7 8 9
revdig[i]=Digits[d]; //d index in Digits
v=v/10;
i=i+1;
}while(v!=0);
//revdig 321
//index 0123
// |-----i
/*
0 1 2 3 4
revdig |5|4|3|2|1|x
|----i
*/
unsigned j=0; //index in s
while(i>0)
{
......@@ -178,8 +178,18 @@ void String::to(unsigned v,char s[])
s[j]=revdig[i];
j=j+1;
}
//s=123
// |---j
/* C/C++
while(i>0)
{
s[j++]=revdig[--j]
| |------- pre decrement
|------------------ post increment
}
*/
/*
s=|1|2|3|4|5|x
|....j
*/
s[j]='\0';
}
......@@ -189,64 +199,64 @@ bool String::to(char s[],unsigned* val)
//|
//s=dec+ at least one decimal digit
//state machine
/*
s=123
v=0
step ch v
0 '1' 1
1 '2' 10*1+2=12
2 '3' 10*12+3=123
4 '\0' 123
*/
unsigned i=0; //index in s
unsigned state=0;
unsigned v=0; //number
unsigned v=0;
while(true)
{
char ch=s[i++];
switch(state)
{
case 0:
{
auto d=isDecDigit(ch);
if (d<0)
{
//a234
return false;
}
v=d;
state=1;
}
case 0://digit expected
{
auto d=isDecDigit(ch);
if (d<0)
{
return false; //not a number
}
v=d;
state=1;
}
break;
case 1:
case 1://digit or terminating zero expected
{
if (ch=='\0')
{
//1\0
*val=v; //by reference
return true;
*val=v;
return true;
}
auto d=isDecDigit(ch);
if (d<0)
{
//111x
return false;
return false;
}
//remain in state
v=10*v+d;
//remain in state 1
}
break;
}
}//end switch
}
} //end while
}
//------------
//test
int main(int argc,char** args)
{
char s[]="123";
char s[]="12345"; //32 bit
unsigned v=0;
auto res=String::to(s,&v);
if (res)
{
std::cout<<"v="<<v<<"\n";
}
else
{
std::cout<<s<<" is not a decimal number\n";
}
std::cout<<"res="<<res<<" v="<<v<<"\n";
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment