Abdellazizhammami / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Stream::findUntil returns incorrect result. #768

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Build the code below and upload the Arduino. (I test the mega board)
2. Open Serial monitor
3. Input the string "aab" in the monitor and send to Arduino.
Actual Result: The sub string "ab" can't be find in the input string
Expected Result: The sub string should be found.

{{{
#include <Arduino.h>

// Defect in bool Stream::findUntil(char *target, size_t targetLen, char 
*terminate, size_t termLen); 
// Input aab in the serial monitor to reproduce the defect.

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(1000);
  if(Serial.available())
  {
      bool bFound = Serial.find("ab", 2);
      Serial.print("Expected Result: 1 <----> Actual Result: ");
      Serial.println(bFound);
  }
}
}}}
What is the expected output? What do you see instead?
Expected output in monitor is : {{{Expected Result: 1 <----> Actual Result: 1}}}
What I see is: {{{Expected Result: 1 <----> Actual Result: 0}}}

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?
Arduino 1.0;    Windows XP; Mega 1280.

Please provide any additional information below.

Change the function as the code below can fix this issue.
{{{

bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t 
termLen)
{
  size_t index = 0;  // maximum target string length is 64k bytes!
  size_t termIndex = 0;
  int c;

  if( *target == 0)
     return true;   // return true if target is a null string
  while( (c = timedRead()) > 0){

    if(c != target[index])
    index = 0; // reset index if any char does not match

    if( c == target[index]){
    //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
      if(++index >= targetLen){ // return true if all chars in the target match
        return true;
      }
    }

    if(termLen > 0 && c == terminator[termIndex]){
       if(++termIndex >= termLen)
         return false;       // return false if terminate string found before target string
    }
    else
        termIndex = 0;
  }
  return false;
}
}}}

Original issue reported on code.google.com by Jeffrey....@gmail.com on 1 Jan 2012 at 11:26

GoogleCodeExporter commented 9 years ago
I fixed this, thanks!  But I also realized that there are more problems, see 
issue 769.

https://github.com/arduino/Arduino/commit/5088b09f2d12a475399778ede58cb958c2aacb
88

Original comment by dmel...@gmail.com on 2 Jan 2012 at 6:58