Wednesday, February 3, 2010

Optimizing bio_display()

Here is the source code:
void bio_display(const char *str, int row, int col, int len){
bio_move(row, col);
if(len <= 0){
bio_putstr(str);
}
else{
int i;
for(i=0; i< len && str[i];i++){
bio_putch(str[i]);
}
for(;i< len;i++){
bio_putch(' ');
}
}
}
To optimize it, I got this solution :
void bio_display(const char *str, int row, int col, int len) {
bio_move(row, col);
if( len <= 0)
bio_putstr(str);
else {
for(;len>0; len--) bio_putch(*str ? *str++ : ' ' );
}
}
From the same logic, it also can be shortened as:
void bio_display(const char *str, int row, int col, int len) {
for((bio_move(row, col), (len <= 0)?bio_putstr(str):0); len>0; len--)
bio_putch(*str ? *str++ : ' ' );
}
I tried to use bio_putstr() instead of bio_putch() which Farad mentioned in the class, but considered the length of the string I insisted to use bio_putch().

No comments:

Post a Comment