Ticket #5203: coding_style.diff

File coding_style.diff, 6.1 KB (added by stippi, 14 years ago)

Patch showing the coding style fixes that were applied.

  • ExpressionTextView.cpp

     
    178178
    179179
    180180void
    181 ExpressionTextView::SetValue(const char* value)
     181ExpressionTextView::SetValue(BString value)
    182182{
    183     // copy the value string so we can modify it
    184     BString val(value);
    185    
    186183    // save the value
    187     fCurrentValue = val;
     184    fCurrentValue = value;
    188185
    189186    // calculate the width of the string
    190187    BFont font;
    191188    uint32 mode = B_FONT_ALL;
    192189    GetFontAndColor(&font, &mode);
    193     float stringWidth = font.StringWidth(val);
     190    float stringWidth = font.StringWidth(value);
    194191   
    195192    // make the string shorter if it does not fit in the view
    196193    float viewWidth = Frame().Width();
    197     if (val.CountChars() > 3 && stringWidth > viewWidth) {
    198        
     194    if (value.CountChars() > 3 && stringWidth > viewWidth) {
    199195        // get the position of the first digit
    200196        int32 firstDigit = 0;
    201         if (val[0] == '-')
     197        if (value[0] == '-')
    202198            firstDigit++;
    203199
    204200        // calculate the value of the exponent
    205201        int32 exponent = 0;
    206         int32 offset = val.FindFirst('.');
     202        int32 offset = value.FindFirst('.');
    207203        if (offset == B_ERROR) {
    208             exponent = val.CountChars() - 1 - firstDigit;
    209             val.Insert('.', 1, firstDigit + 1);
     204            exponent = value.CountChars() - 1 - firstDigit;
     205            value.Insert('.', 1, firstDigit + 1);
    210206        } else {
    211207            if (offset == firstDigit + 1) {
    212208                // if the value is 0.01 or larger then scientific notation
    213209                // won't shorten the string
    214                 if (val[firstDigit] != '0' || val[firstDigit+2] != '0'
    215                     || val[firstDigit+3] != '0') {
     210                if (value[firstDigit] != '0' || value[firstDigit+2] != '0'
     211                    || value[firstDigit + 3] != '0') {
    216212                    exponent = 0;
    217213                } else {
    218214                    // remove the period
    219                     val.Remove(offset, 1);
     215                    value.Remove(offset, 1);
    220216                   
    221217                    // check for negative exponent value
    222218                    exponent = 0;
    223                     while (val[firstDigit] == '0') {
    224                         val.Remove(firstDigit, 1);
     219                    while (value[firstDigit] == '0') {
     220                        value.Remove(firstDigit, 1);
    225221                        exponent--;
    226222                    }
    227223                   
    228                     //add the period
    229                     val.Insert('.', 1, firstDigit + 1);
     224                    // add the period
     225                    value.Insert('.', 1, firstDigit + 1);
    230226                }
    231227            } else {
    232228                // if the period + 1 digit fits in the view scientific notation
    233229                // won't shorten the string
    234                 BString temp = val;
     230                BString temp = value;
    235231                temp.Truncate(offset + 2);
    236232                stringWidth = font.StringWidth(temp);
    237233                if (stringWidth < viewWidth)
    238234                    exponent = 0;
    239235                else {
    240236                    // move the period
    241                     val.Remove(offset, 1);
    242                     val.Insert('.', 1, firstDigit + 1);
     237                    value.Remove(offset, 1);
     238                    value.Insert('.', 1, firstDigit + 1);
    243239                   
    244240                    exponent = offset - (firstDigit + 1);
    245241                }
     
    247243        }
    248244       
    249245        // add the exponent
    250         offset = val.CountChars() - 1;
     246        offset = value.CountChars() - 1;
    251247        if (exponent != 0)
    252             val << "E" << exponent;
     248            value << "E" << exponent;
    253249       
    254250        // reduce the number of digits until the string fits or can not be
    255251        // made any shorter
    256         stringWidth = font.StringWidth(val);
     252        stringWidth = font.StringWidth(value);
    257253        char lastRemovedDigit = '0';
    258254        while (offset > firstDigit && stringWidth > viewWidth) {
    259             if (val[offset] != '.')
    260                 lastRemovedDigit = val[offset];
    261             val.Remove(offset--, 1);
    262             stringWidth = font.StringWidth(val);
     255            if (value[offset] != '.')   
     256                lastRemovedDigit = value[offset];
     257            value.Remove(offset--, 1);
     258            stringWidth = font.StringWidth(value);
    263259        }
    264260       
    265261        // there is no need to keep the period if no digits follow
    266         if (val[offset] == '.') {
    267             val.Remove(offset, 1);
     262        if (value[offset] == '.') {
     263            value.Remove(offset, 1);
    268264            offset--;
    269265        }
    270266       
    271267        // take care of proper rounding of the result
    272         int digit = (int)lastRemovedDigit - 48; // ascii to int
     268        int digit = (int)lastRemovedDigit - '0'; // ascii to int
    273269        if (digit >= 5) {
    274270            for (; offset >= firstDigit; offset--) {
    275                 if (val[offset] == '.')
     271                if (value[offset] == '.')
    276272                    continue;
    277                 digit = (int)(val[offset]) - 47; // ascii to int + 1
    278                 if (digit != 10) break;
    279                 val.Remove(offset, 1);
     273                digit = (int)(value[offset]) - '0' + 1; // ascii to int + 1
     274                if (digit != 10)
     275                    break;
     276                value.Remove(offset, 1);
    280277            }
    281278            if (digit == 10) {
    282279                // carry over, shift the result
    283                 if (val[firstDigit+1] == '.') {
    284                     val[firstDigit+1] = '0';
    285                     val[firstDigit] = '.';
     280                if (value[firstDigit+1] == '.') {
     281                    value[firstDigit+1] = '0';
     282                    value[firstDigit] = '.';
    286283                }
    287                 val.Insert('1', 1, firstDigit);
     284                value.Insert('1', 1, firstDigit);
    288285               
    289                 //remove the exponent value and the last digit
    290                 offset = val.FindFirst('E');
     286                // remove the exponent value and the last digit
     287                offset = value.FindFirst('E');
    291288                if (offset == B_ERROR)
    292                     offset = val.CountChars();
    293                 val.Truncate(--offset);
    294                 offset--; //offset now points to the last digit
     289                    offset = value.CountChars();
     290                value.Truncate(--offset);
     291                offset--; // offset now points to the last digit
    295292               
    296293                // increase the exponent and add it back to the string
    297294                exponent++;
    298                 val << 'E' << exponent;
     295                value << 'E' << exponent;
    299296            } else {
    300297                // increase the current digit value with one
    301                 val[offset] = char(digit + 48);
     298                value[offset] = char(digit + 48);
    302299            }
    303300        }
    304301       
    305302        // remove trailing zeros
    306         while (val[offset] == '0')
    307             val.Remove(offset--, 1);
     303        while (value[offset] == '0')
     304            value.Remove(offset--, 1);
    308305       
    309306        // there is no need to keep the period if no digits follow
    310         if (val[offset] == '.')
    311             val.Remove(offset, 1);
     307        if (value[offset] == '.')
     308            value.Remove(offset, 1);
    312309    }
    313310
    314311    // set the new value   
    315     SetExpression(val);
     312    SetExpression(value);
    316313}
    317314
    318315
  • ExpressionTextView.h

     
    4242            void                AddKeypadLabel(const char* label);
    4343
    4444            void                SetExpression(const char* expression);
    45             void                SetValue(const char* value);
     45            void                SetValue(BString value);
    4646
    4747            void                BackSpace();
    4848            void                Clear();