strpos NOT doing what I'd expect - Mailing list pgsql-general

From Ralph Smith
Subject strpos NOT doing what I'd expect
Date
Msg-id 37C882AE-F4C4-4770-86D9-C4219694BDF7@washington.edu
Whole thread Raw
Responses Re: strpos NOT doing what I'd expect  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: strpos NOT doing what I'd expect  (Alban Hertroys <dalroi@solfertje.student.utwente.nl>)
List pgsql-general
CODE:
===============================
CREATE OR REPLACE FUNCTION find_next_delim(invar varchar, delimlist varchar) RETURNS integer AS
$$

/*   OVERLOADED Function.  The other version takes a 3rd parameter as the
                starting position in invar.
*/

DECLARE
  
  achar  character := '' ;
  j      int       := 0  ;
  
BEGIN

  IF length(delimlist) = 0 THEN
    RAISE NOTICE 'In function \'find_next_delim\' the delimiter cannot be null.' ;
  END IF ;

  
  FOR i IN 1 .. length(invar) 
  LOOP

    j := j + 1 ;
    achar := substring(invar from i for 1 ) ;
    RAISE NOTICE 'achar is R%S',achar ;
    IF strpos(delimlist,achar) <> 0 THEN
      RETURN j ;
    END IF ;
    
  END LOOP ;
  
  RETURN 0 ;
  
END ;
$$ LANGUAGE plpgsql ;  /*   find_next_delim   */




WHAT'S HAPPENING:
===============================
airburst=# select find_next_delim('ralph smith','3') ;

NOTICE:  achar is RrS
NOTICE:  achar is RaS
NOTICE:  achar is RlS
NOTICE:  achar is RpS
NOTICE:  achar is RhS
NOTICE:  achar is R S
 find_next_delim 
-----------------
               6
(1 row)


airburst=# select find_next_delim('ralph smith','') ;  -- for the heck of it, that's a null

NOTICE:  In function 'find_next_delim' the delimiter cannot be null.
NOTICE:  achar is RrS
NOTICE:  achar is RaS
NOTICE:  achar is RlS
NOTICE:  achar is RpS
NOTICE:  achar is RhS
NOTICE:  achar is R S
 find_next_delim 
-----------------
               6
(1 row)

WHY find a match on the space???

Thanks!

pgsql-general by date:

Previous
From: Celso Pinto
Date:
Subject: array column and b-tree index allowing only 8191 bytes
Next
From: Tom Lane
Date:
Subject: Re: PL/pgSQL graph enumeration function hangs