user21286: As written in the title, could you tell me where to find the source code of the function "Serial.find()"?
I haven' t installed the Arduino IDE, so I'm glad if you could me link a web resource, or if you directly copy the source code here .
Thanks for your time.

user4143: It is part of the Stream class which the Serial class inherits from.

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L93

It just calls findUntil() which is a little further down in the same file:

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L114

That itself calls findMulti() which is where all the magic happens:

https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L256


int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
  // any zero length target string automatically matches and would make
  // a mess of the rest of the algorithm.
  for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
    if (t->len <= 0)
      return t - targets;
  }

  while (1) {
    int c = timedRead();
    if (c < 0)
      return -1;

    for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
      // the simple case is if we match, deal with that first.
      if (c == t->str[t->index]) {
        if (++t->index == t->len)
          return t - targets;
        else
          continue;
      }

      // if not we need to walk back and see if we could have matched further
      // down the stream (ie '1112' doesn't match the first position in '11112'
      // but it will match the second position so we can't just reset the current
      // index to 0 when we find a mismatch.
      if (t->index == 0)
        continue;

      int origIndex = t->index;
      do {
        --t->index;
        // first check if current char works against the new current index
        if (c != t->str[t->index])
          continue;

        // if it's the only char then we're good, nothing more to check
        if (t->index == 0) {
          t->index++;
          break;
        }

        // otherwise we need to check the rest of the found string
        int diff = origIndex - t->index;
        size_t i;
        for (i = 0; i < t->index; ++i) {
          if (t->str[i] != t->str[i + diff])
            break;
        }

        // if we successfully got through the previous loop then our current
        // index is good.
        if (i == t->index) {
          t->index++;
          break;
        }

        // otherwise we just try the next index
      } while (t->index);
    }
  }
  // unreachable
  return -1;
}


21286: <p>As written in the title, could you tell me where to find the source code of the function "Serial.find()"?</p>

<p>I haven' t installed the Arduino IDE, so I'm glad if you could me link a web resource, or if you directly copy the source code here .</p>

<p>Thanks for your time.</p>
 4143: <p>It is part of the Stream class which the Serial class inherits from.</p>

<ul>
<li><a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L93" rel="nofollow">https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L93</a></li>
</ul>

<p>It just calls <code>findUntil()</code> which is a little further down in the same file:</p>

<ul>
<li><a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L114" rel="nofollow">https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L114</a></li>
</ul>

<p>That itself calls <code>findMulti()</code> which is where all the magic happens:</p>

<ul>
<li><a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L256" rel="nofollow">https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Stream.cpp#L256</a></li>
</ul>

<hr>

<pre><code>int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
  // any zero length target string automatically matches and would make
  // a mess of the rest of the algorithm.
  for (struct MultiTarget *t = targets; t &lt; targets+tCount; ++t) {
    if (t-&gt;len &lt;= 0)
      return t - targets;
  }

  while (1) {
    int c = timedRead();
    if (c &lt; 0)
      return -1;

    for (struct MultiTarget *t = targets; t &lt; targets+tCount; ++t) {
      // the simple case is if we match, deal with that first.
      if (c == t-&gt;str[t-&gt;index]) {
        if (++t-&gt;index == t-&gt;len)
          return t - targets;
        else
          continue;
      }

      // if not we need to walk back and see if we could have matched further
      // down the stream (ie '1112' doesn't match the first position in '11112'
      // but it will match the second position so we can't just reset the current
      // index to 0 when we find a mismatch.
      if (t-&gt;index == 0)
        continue;

      int origIndex = t-&gt;index;
      do {
        --t-&gt;index;
        // first check if current char works against the new current index
        if (c != t-&gt;str[t-&gt;index])
          continue;

        // if it's the only char then we're good, nothing more to check
        if (t-&gt;index == 0) {
          t-&gt;index++;
          break;
        }

        // otherwise we need to check the rest of the found string
        int diff = origIndex - t-&gt;index;
        size_t i;
        for (i = 0; i &lt; t-&gt;index; ++i) {
          if (t-&gt;str[i] != t-&gt;str[i + diff])
            break;
        }

        // if we successfully got through the previous loop then our current
        // index is good.
        if (i == t-&gt;index) {
          t-&gt;index++;
          break;
        }

        // otherwise we just try the next index
      } while (t-&gt;index);
    }
  }
  // unreachable
  return -1;
}
</code></pre>
