From b98e4af5f09906809e7fec06b650451627685470 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Tue, 2 Nov 2004 01:10:35 +0000 Subject: [PATCH] Rewrite username validation in todo example to workaround current bug in parser combinator library. --- contrib/cont-responder/todo-example.factor | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/contrib/cont-responder/todo-example.factor b/contrib/cont-responder/todo-example.factor index f43d86e51a..5d02acb8f0 100644 --- a/contrib/cont-responder/todo-example.factor +++ b/contrib/cont-responder/todo-example.factor @@ -191,16 +191,29 @@ USE: parser-combinators "Register" login-form ] simple-page ; -: username-parser ( -- parser ) - #! Return a parser which parses a valid todo username. - #! That is, it contains only lowercase, uppercase and digits. - [ letter? ] satisfy - [ LETTER? ] satisfy <|> - [ digit? ] satisfy <|> just ; +: valid-username-char ( ch -- b ) + #! Return true if the character is valid to appear in a username. + [ + [ letter? ] [ drop t ] + [ LETTER? ] [ drop t ] + [ digit? ] [ drop t ] + [ ] [ drop f ] + ] cond ; -: is-valid-username? ( password -- bool ) +: replace-invalid-username-chars ( str -- str ) + #! Return a string with invalid username characters mapped to underscores. + [ + dup valid-username-char [ + ] [ + drop CHAR: _ + ] ifte + ] str-map ; + +: testx ( string -- b ) dup replace-invalid-username-chars = ; + +: is-valid-username? ( username -- bool ) #! Return true if the username parses correctly - username-parser call ; + dup replace-invalid-username-chars = ; : login-details-valid? ( name password -- ) #! Ensure that a valid username and password were