Changeset 556:2d1a26628a09

Show
Ignore:
Timestamp:
06/01/08 07:22:00 (7 months ago)
Author:
David Anderson <dave@…>
Branch:
default
Message:

Replace strcmp() and strncmp() with streq() and streqn().

We never used the total ordering of strcmp(), we always test for
equality. We might as well use functions specialized in that, with
less that can go wrong.

Location:
nxos
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • nxos/base/_fs.c

    r551 r556  
    5454             FS_FILENAME_LENGTH); 
    5555 
    56       if (strcmp(nameconv.chars, name) == 0) { 
     56      if (streq(nameconv.chars, name)) { 
    5757        *origin = i; 
    5858        return FS_ERR_NO_ERROR; 
  • nxos/base/drivers/radar.c

    r499 r556  
    9595 
    9696  return nx_radar_read(sensor, RADAR_SENSOR_TYPE, type) 
    97     && strncmp((char *)type, RADAR_LEGO_SENSOR_TYPE, 
    98                strlen(RADAR_LEGO_SENSOR_TYPE)) == 0; 
     97    && streq((char *)type, RADAR_LEGO_SENSOR_TYPE); 
    9998} 
    10099 
  • nxos/base/lib/rcmd/_rcmd.c

    r551 r556  
    4040 
    4141  while (rcmd_commands[i].name) { 
    42     if (strncmp(line, rcmd_commands[i].name, 
    43                 strlen(rcmd_commands[i].name)) == 0) { 
     42    if (streq(line, rcmd_commands[i].name)) { 
    4443      *command = rcmd_commands[i]; 
    4544      return RCMD_ERR_NO_ERROR; 
     
    165164  } 
    166165 
    167   if (ntokens == 4 && strncmp(line + indices[3], "sync", 4) == 0) { 
     166  if (ntokens == 4 && streq(line + indices[3], "sync") == 0) { 
    168167    nx_sound_freq(freq, duration); 
    169168  } else { 
  • nxos/base/util.c

    r554 r556  
    4444} 
    4545 
    46 U32 strncmp(const char *a, const char *b, U32 n) { 
    47   U32 i; 
    48  
     46bool streqn(const char *a, const char *b, U32 n) { 
    4947  NX_ASSERT(a != NULL && b != NULL); 
    5048 
    51   for (i=0 ; i<n ; i++) { 
    52     if (a[i] < b[i]) { 
    53       return -1; 
    54     } else if (a[i] > b[i]) { 
    55       return 1; 
    56     } 
     49  while (n--) { 
     50    if (*a != *b++) 
     51      return FALSE; 
     52    if (*a++ == '\0') 
     53      break; 
    5754  } 
    5855 
    59   return 0; 
     56  return TRUE; 
    6057} 
    6158 
    62 U32 strcmp(const char *a, const char *b) { 
    63   return strncmp(a, b, MIN(strlen(a), strlen(b)+1)); 
     59bool streq(const char *a, const char *b) { 
     60  NX_ASSERT(a != NULL && b != NULL); 
     61 
     62  while (*a != '\0' && *b != '\0') { 
     63    if (*a++ != *b++) 
     64      return FALSE; 
     65  } 
     66 
     67  return *a == *b ? TRUE : FALSE; 
    6468} 
    6569 
  • nxos/base/util.h

    r547 r556  
    5858U32 strlen(const char *str); 
    5959 
    60 /** Order two strings based on a limited amount of input. 
     60/** Compare two string prefixes for equality. 
    6161 * 
    6262 * @param a First string to compare. 
    6363 * @param b Second string to compare. 
    6464 * @param n Number of bytes to compare. 
    65  * @return -1 if @a a sorts before @a b, 1 if @a b sorts before @a a, 0 
    66  *         if both are equivalent up to @a n bytes. 
     65 * @return TRUE if the first @a n bytes of @a a are equal to @a b, 
     66 * FALSE otherwise. 
     67 * 
     68 * @note This function will halt on the first NULL byte it finds in 
     69 * either string. 
    6770 */ 
    68 U32 strncmp(const char *a, const char *b, U32 n); 
     71bool streqn(const char *a, const char *b, U32 n); 
    6972 
    70 /** Order two strings. 
     73/** compare two strings for equality. 
    7174 * 
    72  * This is equivalent to @a strncmp, with the maximum length being the 
     75 * This is equivalent to @a strneq, with the maximum length being the 
    7376 * length of the shortest input string. 
    7477 * 
    75  * @see strncmp 
     78 * @see strneq 
    7679 */ 
    77 U32 strcmp(const char *a, const char *b); 
     80bool streq(const char *a, const char *b); 
    7881 
    7982/** Locate leftmost instance of character @a c in string @a s. 
  • nxos/systems/tests/main.c

    r518 r556  
    1919 
    2020  //tests_all(); 
    21   tests_usb(); 
     21  //tests_usb(); 
    2222  //tests_bt(); 
    2323  //tests_usb_hardcore(); 
  • nxos/systems/tests/tests.c

    r502 r556  
    44#include "base/interrupts.h" 
    55#include "base/display.h" 
     6#include "base/assert.h" 
    67#include "base/memmap.h" 
    78#include "base/util.h" 
     
    4243} 
    4344 
     45 
     46void tests_util(void) { 
     47  hello(); 
     48 
     49  /* Simple equality. */ 
     50  NX_ASSERT(streq("foo", "foo")); 
     51 
     52  /* Simple inequality. */ 
     53  NX_ASSERT(!streq("foo", "bar")); 
     54  NX_ASSERT(!streq("bar", "foo")); 
     55 
     56  /* Inequality towards the end of the string. */ 
     57  NX_ASSERT(!streq("foo", "fob")); 
     58  NX_ASSERT(!streq("fob", "foo")); 
     59 
     60  /* Inequality of different length strings. */ 
     61  NX_ASSERT(!streq("foo", "foobar")); 
     62  NX_ASSERT(!streq("foobar", "foo")); 
     63 
     64  /* Inequality vs. the empty string. */ 
     65  NX_ASSERT(!streq("foo", "")); 
     66  NX_ASSERT(!streq("", "foo")); 
     67 
     68  /* The border case of the empty string. */ 
     69  NX_ASSERT(streq("", "")); 
     70 
     71 
     72  /* Simple equality. */ 
     73  NX_ASSERT(streqn("foo", "foo", 3)); 
     74 
     75  /* Simple inequality. */ 
     76  NX_ASSERT(!streqn("foo", "bar", 3)); 
     77  NX_ASSERT(!streqn("bar", "foo", 3)); 
     78 
     79  /* Inequality towards the end of the string. */ 
     80  NX_ASSERT(!streqn("foo", "fob", 3)); 
     81  NX_ASSERT(!streqn("fob", "foo", 3)); 
     82 
     83  /* Inequality of different length strings. */ 
     84  NX_ASSERT(!streqn("foo", "foobar", 6)); 
     85  NX_ASSERT(!streqn("foobar", "foo", 6)); 
     86 
     87  /* Inequality vs. the empty string. */ 
     88  NX_ASSERT(!streqn("foo", "", 3)); 
     89  NX_ASSERT(!streqn("", "foo", 3)); 
     90 
     91  /* Equality of the empty string, no matter the given length. */ 
     92  NX_ASSERT(streqn("", "", 42)); 
     93 
     94  /* Equality of unequal strings if length == 0 */ 
     95  NX_ASSERT(streqn("bleh", "foo", 0)); 
     96 
     97  /* Prefix equality of unequal strings */ 
     98  NX_ASSERT(streqn("feh", "foo", 1)); 
     99 
     100  goodbye(); 
     101} 
     102 
     103 
    44104void tests_display(void) { 
    45105  char buf[2] = { 0, 0 }; 
     
    327387} 
    328388 
    329 void tests_bt2(void) 
    330 { 
     389void tests_bt2(void) { 
    331390  /*int i; 
    332391   */ 
     
    387446 
    388447 
    389 /* returns 1 if they are identic 
    390  * 0 else 
    391  * 
    392  * TODO: use base/util.h:strncmp instead. 
    393  */ 
    394 static U8 compare_str(char *str_a, char *str_b, U32 max) 
    395 { 
    396  
    397   while (*str_a != '\0' 
    398          && *str_b != '\0' 
    399          && max > 0) 
    400     { 
    401       if (*str_a != *str_b) { 
    402         return 0; 
    403       } 
    404       str_a++; 
    405       str_b++; 
    406       max--; 
    407     } 
    408  
    409   if (*str_a != *str_b && max > 0) { 
    410     return 0; 
    411   } 
    412  
    413   return 1; 
    414 } 
    415  
    416  
    417448#define CMD_UNKNOWN    "Unknown" 
    418449#define CMD_OK         "Ok" 
     
    426457 * @return 0 if success ; 1 if unknown command ; 2 if halt 
    427458 */ 
    428 static int tests_command(char *buffer, int lng) 
    429 { 
     459static int tests_command(char *buffer) { 
    430460  int i; 
    431461  S32 t; 
     
    434464 
    435465  i = 0; 
    436   if (compare_str(buffer, "motor", lng)) 
     466  if (streq(buffer, "motor")) 
    437467    tests_motor(); 
    438   else if (compare_str(buffer, "sound", lng)) 
     468  else if (streq(buffer, "sound")) 
    439469    tests_sound(); 
    440   else if (compare_str(buffer, "display", lng)) 
     470  else if (streq(buffer, "util")) 
     471    tests_util(); 
     472  else if (streq(buffer, "display")) 
    441473    tests_display(); 
    442   else if (compare_str(buffer, "sysinfo", lng)) 
     474  else if (streq(buffer, "sysinfo")) 
    443475    tests_sysinfo(); 
    444   else if (compare_str(buffer, "sensors", lng)) 
     476  else if (streq(buffer, "sensors")) 
    445477    tests_sensors(); 
    446   else if (compare_str(buffer, "tachy", lng)) 
     478  else if (streq(buffer, "tachy")) 
    447479    tests_tachy(); 
    448   else if (compare_str(buffer, "radar", lng)) 
     480  else if (streq(buffer, "radar")) 
    449481    tests_radar(); 
    450   else if (compare_str(buffer, "bt", lng)) 
     482  else if (streq(buffer, "bt")) 
    451483    tests_bt(); 
    452   else if (compare_str(buffer, "bt2", lng)) 
     484  else if (streq(buffer, "bt2")) 
    453485    tests_bt2(); 
    454   else if (compare_str(buffer, "all", lng)) 
     486  else if (streq(buffer, "all")) 
    455487    tests_all(); 
    456   else if (compare_str(buffer, "halt", lng)) 
     488  else if (streq(buffer, "halt")) 
    457489    return 2; 
    458   else if (compare_str(buffer, "Al", lng)) 
     490  else if (streq(buffer, "Al")) 
    459491    nx_motors_rotate_angle(0, 90, 100, 1); 
    460   else if (compare_str(buffer, "Ar", lng)) 
     492  else if (streq(buffer, "Ar")) 
    461493    nx_motors_rotate_angle(0, -90, 100, 1); 
    462   else if (compare_str(buffer, "Ac", lng)) { 
     494  else if (streq(buffer, "Ac")) { 
    463495    nx_motors_rotate(0, 75); 
    464496    while((t = nx_motors_get_tach_count(0)) != 0) { 
     
    473505    } 
    474506    nx_motors_stop(0, 1); 
    475   } else if (compare_str(buffer, "BCf", lng)) { 
     507  } else if (streq(buffer, "BCf")) { 
    476508    nx_motors_rotate(1, -100); 
    477509    nx_motors_rotate(2, -100); 
     
    479511    nx_motors_stop(1, 0); 
    480512    nx_motors_stop(2, 0); 
    481   } else if (compare_str(buffer, "BCr", lng)) { 
     513  } else if (streq(buffer, "BCr")) { 
    482514    nx_motors_rotate(1, 80); 
    483515    nx_motors_rotate(2, 80); 
     
    588620    /* Start interpreting */ 
    589621 
    590     i = tests_command(buffer, lng); 
     622    i = tests_command(buffer); 
    591623 
    592624    nx_bt_stream_read((U8 *)&buffer, 2); 
     
    664696    /* Start interpreting */ 
    665697 
    666     i = tests_command(buffer, lng); 
     698    i = tests_command(buffer); 
    667699 
    668700    if (i == 2) { 
     
    701733 
    702734    if ( (lng = nx_usb_data_read()) > 0) { 
    703       if (compare_str(buffer, "halt", lng)) { 
     735      if (streq(buffer, "halt")) { 
    704736        break; 
    705737      } 
     
    769801  test_silent = TRUE; 
    770802 
     803  tests_util(); 
    771804  tests_display(); 
    772805  tests_sound(); 
  • nxos/systems/tests/tests.h

    r489 r556  
    22#define __NXOS_TESTS_H__ 
    33 
     4void tests_util(void); 
    45void tests_motor(void); 
    56void tests_sound(void);