hashtables were sized wrong; rename str- words to string-

cvs
Slava Pestov 2005-03-05 21:33:40 +00:00
parent f0dfb77690
commit a74632b243
50 changed files with 231 additions and 242 deletions

View File

@ -61,7 +61,6 @@
+ kernel: + kernel:
- hash-size regression
- vectors: ensure its ok with bignum indices - vectors: ensure its ok with bignum indices
- cat, reverse-cat primitives - cat, reverse-cat primitives
- code gc - code gc

View File

@ -72,7 +72,7 @@ USE: logging
: escape-quotes ( string -- string ) : escape-quotes ( string -- string )
#! Replace occurrences of single quotes with #! Replace occurrences of single quotes with
#! backslash quote. #! backslash quote.
[ dup [ [[ CHAR: ' "\\'" ]] [[ CHAR: " "\\\"" ]] ] assoc dup rot ? ] str-map ; [ dup [ [[ CHAR: ' "\\'" ]] [[ CHAR: " "\\\"" ]] ] assoc dup rot ? ] string-map ;
: make-eval-javascript ( string -- string ) : make-eval-javascript ( string -- string )
#! Give a string return some javascript that when #! Give a string return some javascript that when

View File

@ -208,7 +208,7 @@ USE: kernel
] [ ] [
drop CHAR: _ drop CHAR: _
] ifte ] ifte
] str-map ; ] string-map ;
: is-valid-username? ( username -- bool ) : is-valid-username? ( username -- bool )
#! Return true if the username parses correctly #! Return true if the username parses correctly
@ -334,11 +334,11 @@ USE: kernel
: priority-valid? ( string -- bool ) : priority-valid? ( string -- bool )
#! Test the string containing a priority to see if it is #! Test the string containing a priority to see if it is
#! valid. It should be a single digit from 0-9. #! valid. It should be a single digit from 0-9.
dup str-length 1 = [ 0 swap str-nth digit? ] [ drop f ] ifte ; dup string-length 1 = [ 0 swap string-nth digit? ] [ drop f ] ifte ;
: todo-details-valid? ( priority description -- bool ) : todo-details-valid? ( priority description -- bool )
#! Return true if a valid priority and description were entered. #! Return true if a valid priority and description were entered.
str-length 0 > [ priority-valid? ] [ drop f ] ifte ; string-length 0 > [ priority-valid? ] [ drop f ] ifte ;
: get-new-todo-item ( -- <todo-item> ) : get-new-todo-item ( -- <todo-item> )
#! Enter a new item to the current todo list. #! Enter a new item to the current todo list.

View File

@ -151,7 +151,7 @@ USE: hashtables
: priority-comparator ( item1 item2 -- bool ) : priority-comparator ( item1 item2 -- bool )
#! Return true if item1 is a higher priority than item2 #! Return true if item1 is a higher priority than item2
>r item-priority r> item-priority str-lexi> ; >r item-priority r> item-priority string> ;
: todo-items ( <todo> -- alist ) : todo-items ( <todo> -- alist )
#! Return a list of items for the given todo list. #! Return a list of items for the given todo list.

View File

@ -172,7 +172,7 @@ END-STRUCT
#! Prepare a SQL statement. Returns the statement which #! Prepare a SQL statement. Returns the statement which
#! can have values bound to parameters or simply executed. #! can have values bound to parameters or simply executed.
#! TODO: Support multiple statements in the SQL string. #! TODO: Support multiple statements in the SQL string.
dup str-length <sqlite3-stmt-indirect> dup >r dup string-length <sqlite3-stmt-indirect> dup >r
<char*-indirect> sqlite3_prepare sqlite-check-result <char*-indirect> sqlite3_prepare sqlite-check-result
r> sqlite3-stmt-indirect-pointer ; r> sqlite3-stmt-indirect-pointer ;

View File

@ -7,16 +7,16 @@ USE: test
: decimal-split ( string -- string string ) : decimal-split ( string -- string string )
#! Split a string before and after the decimal point. #! Split a string before and after the decimal point.
dup "." index-of dup -1 = [ drop f ] [ str// ] ifte ; dup "." index-of dup -1 = [ drop f ] [ string// ] ifte ;
: decimal-tail ( count str -- string ) : decimal-tail ( count str -- string )
#! Given a decimal, trims all but a count of decimal places. #! Given a decimal, trims all but a count of decimal places.
[ str-length min ] keep str-head ; [ string-length min ] keep string-head ;
: decimal-cat ( before after -- string ) : decimal-cat ( before after -- string )
#! If after is of zero length, return before, otherwise #! If after is of zero length, return before, otherwise
#! return "before.after". #! return "before.after".
dup str-length 0 = [ dup string-length 0 = [
drop drop
] [ ] [
"." swap cat3 "." swap cat3

View File

@ -77,7 +77,7 @@ M: privmsg irc-display ( line -- )
: say ( line -- ) : say ( line -- )
channel get [ (msg) ] [ "No channel." print ] ifte* ; channel get [ (msg) ] [ "No channel." print ] ifte* ;
: talk ( input -- ) "/" ?str-head [ command ] [ say ] ifte ; : talk ( input -- ) "/" ?string-head [ command ] [ say ] ifte ;
: talk-loop ( -- ) read [ talk talk-loop ] when* ; : talk-loop ( -- ) read [ talk talk-loop ] when* ;
: irc ( nick server -- ) : irc ( nick server -- )

View File

@ -8,7 +8,7 @@ USING: vectors kernel math stdio strings ;
} vector-nth >r 4 * dup 4 + r> substring ; } vector-nth >r 4 * dup 4 + r> substring ;
: lcd-row ( num row -- ) : lcd-row ( num row -- )
swap [ CHAR: 0 - over lcd-digit write ] str-each drop ; swap [ CHAR: 0 - over lcd-digit write ] string-each drop ;
: lcd ( num -- str ) : lcd ( num -- str )
3 [ 2dup lcd-row terpri ] repeat drop ; 3 [ 2dup lcd-row terpri ] repeat drop ;

View File

@ -233,16 +233,17 @@ M: cons ' ( c -- tagged )
( Strings ) ( Strings )
: align-string ( n str -- ) : align-string ( n str -- )
tuck str-length - CHAR: \0 fill cat2 ; tuck string-length - CHAR: \0 fill cat2 ;
: emit-chars ( str -- ) : emit-chars ( str -- )
"big-endian" get [ str-reverse ] unless string>list "big-endian" get [ reverse ] unless
0 swap [ swap 16 shift + ] str-each emit ; 0 swap [ swap 16 shift + ] each emit ;
: (pack-string) ( n list -- ) : (pack-string) ( n list -- )
#! Emit bytes for a string, with n characters per word. #! Emit bytes for a string, with n characters per word.
[ [
2dup str-length > [ dupd align-string ] when emit-chars 2dup string-length > [ dupd align-string ] when
emit-chars
] each drop ; ] each drop ;
: pack-string ( string -- ) : pack-string ( string -- )
@ -251,7 +252,7 @@ M: cons ' ( c -- tagged )
: emit-string ( string -- ) : emit-string ( string -- )
object-tag here-as swap object-tag here-as swap
string-type >header emit string-type >header emit
dup str-length emit-fixnum dup string-length emit-fixnum
dup hashcode emit-fixnum dup hashcode emit-fixnum
"\0" cat2 pack-string "\0" cat2 pack-string
align-here ; align-here ;

View File

@ -30,20 +30,18 @@ vocabularies get [
[ "ifte" "kernel" [ [ object general-list general-list ] [ ] ] ] [ "ifte" "kernel" [ [ object general-list general-list ] [ ] ] ]
[ "cons" "lists" [ [ object object ] [ cons ] ] ] [ "cons" "lists" [ [ object object ] [ cons ] ] ]
[ "<vector>" "vectors" [ [ integer ] [ vector ] ] ] [ "<vector>" "vectors" [ [ integer ] [ vector ] ] ]
[ "str-nth" "strings" [ [ integer string ] [ integer ] ] ] [ "string-nth" "strings" [ [ integer string ] [ integer ] ] ]
[ "str-compare" "strings" [ [ string string ] [ integer ] ] ] [ "string-compare" "strings" [ [ string string ] [ integer ] ] ]
[ "str=" "strings" [ [ string string ] [ boolean ] ] ] [ "string=" "strings" [ [ string string ] [ boolean ] ] ]
[ "index-of*" "strings" [ [ integer string text ] [ integer ] ] ] [ "index-of*" "strings" [ [ integer string text ] [ integer ] ] ]
[ "substring" "strings" [ [ integer integer string ] [ string ] ] ] [ "substring" "strings" [ [ integer integer string ] [ string ] ] ]
[ "str-reverse" "strings" [ [ string ] [ string ] ] ]
[ "<sbuf>" "strings" [ [ integer ] [ sbuf ] ] ] [ "<sbuf>" "strings" [ [ integer ] [ sbuf ] ] ]
[ "sbuf-length" "strings" [ [ sbuf ] [ integer ] ] ] [ "sbuf-length" "strings" [ [ sbuf ] [ integer ] ] ]
[ "set-sbuf-length" "strings" [ [ integer sbuf ] [ ] ] ] [ "set-sbuf-length" "strings" [ [ integer sbuf ] [ ] ] ]
[ "sbuf-nth" "strings" [ [ integer sbuf ] [ integer ] ] ] [ "sbuf-nth" "strings" [ [ integer sbuf ] [ integer ] ] ]
[ "set-sbuf-nth" "strings" [ [ integer integer sbuf ] [ ] ] ] [ "set-sbuf-nth" "strings" [ [ integer integer sbuf ] [ ] ] ]
[ "sbuf-append" "strings" [ [ text sbuf ] [ ] ] ] [ "sbuf-append" "strings" [ [ text sbuf ] [ ] ] ]
[ "sbuf>str" "strings" [ [ sbuf ] [ string ] ] ] [ "sbuf>string" "strings" [ [ sbuf ] [ string ] ] ]
[ "sbuf-reverse" "strings" [ [ sbuf ] [ ] ] ]
[ "sbuf-clone" "strings" [ [ sbuf ] [ sbuf ] ] ] [ "sbuf-clone" "strings" [ [ sbuf ] [ sbuf ] ] ]
[ "sbuf=" "strings" [ [ sbuf sbuf ] [ boolean ] ] ] [ "sbuf=" "strings" [ [ sbuf sbuf ] [ boolean ] ] ]
[ "arithmetic-type" "math-internals" [ [ object object ] [ object object fixnum ] ] ] [ "arithmetic-type" "math-internals" [ [ object object ] [ object object fixnum ] ] ]
@ -195,7 +193,7 @@ vocabularies get [
[ "next-object" "memory" [ [ ] [ object ] ] ] [ "next-object" "memory" [ [ ] [ object ] ] ]
[ "end-scan" "memory" [ [ ] [ ] ] ] [ "end-scan" "memory" [ [ ] [ ] ] ]
[ "size" "memory" [ [ object ] [ fixnum ] ] ] [ "size" "memory" [ [ object ] [ fixnum ] ] ]
] [ ] [
3unlist >r create >r 1 + r> 2dup swap f define r> 3unlist >r create >r 1 + r> 2dup swap f define r>
dup string? [ dup string? [
"stack-effect" set-word-prop "stack-effect" set-word-prop

View File

@ -22,7 +22,7 @@ kernel-internals ;
: cli-var-param ( name value -- ) swap ":" split set-path ; : cli-var-param ( name value -- ) swap ":" split set-path ;
: cli-bool-param ( name -- ) "no-" ?str-head not put ; : cli-bool-param ( name -- ) "no-" ?string-head not put ;
: cli-param ( param -- ) : cli-param ( param -- )
#! Handle a command-line argument starting with '-' by #! Handle a command-line argument starting with '-' by
@ -36,7 +36,7 @@ kernel-internals ;
: cli-arg ( argument -- argument ) : cli-arg ( argument -- argument )
#! Handle a command-line argument. If the argument was #! Handle a command-line argument. If the argument was
#! consumed, returns f. Otherwise returns the argument. #! consumed, returns f. Otherwise returns the argument.
dup f-or-"" [ "-" ?str-head [ cli-param f ] when ] unless ; dup f-or-"" [ "-" ?string-head [ cli-param f ] when ] unless ;
: parse-switches ( args -- args ) : parse-switches ( args -- args )
[ cli-arg ] map ; [ cli-arg ] map ;

View File

@ -9,6 +9,8 @@ DEFER: set-hash-size
IN: hashtables IN: hashtables
USING: generic kernel lists math vectors ; USING: generic kernel lists math vectors ;
! We put hash-size in the hashtables vocabulary, and
! the other words in kernel-internals.
BUILTIN: hashtable 10 BUILTIN: hashtable 10
[ 1 "hash-size" set-hash-size ] [ 1 "hash-size" set-hash-size ]
[ 2 hash-array set-hash-array ] ; [ 2 hash-array set-hash-array ] ;
@ -35,25 +37,19 @@ IN: kernel-internals
[ array-nth swap call ] 2keep [ array-nth swap call ] 2keep
set-array-nth ; inline set-array-nth ; inline
: hash-size+ ( hash -- ) dup hash-size 1 + swap set-hash-size ;
: hash-size- ( hash -- ) dup hash-size 1 - swap set-hash-size ;
IN: hashtables IN: hashtables
: hash-size+ ( hash -- ) : bucket-count ( hash -- n ) hash-array array-capacity ;
dup hash-size 1 + swap set-hash-size ;
: hash-size- ( hash -- )
dup hash-size 1 - swap set-hash-size ;
: bucket-count ( hash -- n )
hash-array array-capacity ;
: (hashcode) ( key table -- index ) : (hashcode) ( key table -- index )
#! Compute the index of the bucket for a key. #! Compute the index of the bucket for a key.
>r hashcode r> bucket-count rem ; inline >r hashcode r> bucket-count rem ; inline
: hash* ( key table -- [[ key value ]] ) : hash* ( key table -- [[ key value ]] )
#! Look up a value in the hashtable. First the bucket is #! Look up a value in the hashtable.
#! determined using the hash function, then the association
#! list therein is searched linearly.
2dup (hashcode) swap hash-bucket assoc* ; 2dup (hashcode) swap hash-bucket assoc* ;
: hash ( key table -- value ) : hash ( key table -- value )
@ -68,7 +64,7 @@ IN: hashtables
-rot 2dup (hashcode) over [ -rot 2dup (hashcode) over [
( quot key hash assoc -- ) ( quot key hash assoc -- )
swapd 2dup swapd 2dup
assoc [ rot hash-size- ] [ rot drop ] ifte assoc* [ rot hash-size- ] [ rot drop ] ifte
rot call rot call
] change-bucket ; inline ] change-bucket ; inline
@ -97,7 +93,9 @@ IN: hashtables
: rehash ( hash -- ) : rehash ( hash -- )
#! Increase the hashtable size if its too small. #! Increase the hashtable size if its too small.
dup rehash? [ dup rehash? [
dup hash>alist over grow-hash dup hash>alist
over grow-hash
0 pick set-hash-size
[ unswons rot (set-hash) ] each-with [ unswons rot (set-hash) ] each-with
] [ ] [
drop drop
@ -115,6 +113,7 @@ IN: hashtables
: hash-clear ( hash -- ) : hash-clear ( hash -- )
#! Remove all entries from a hashtable. #! Remove all entries from a hashtable.
0 over set-hash-size
dup bucket-count [ dup bucket-count [
[ f swap pick set-hash-bucket ] keep [ f swap pick set-hash-bucket ] keep
] repeat drop ; ] repeat drop ;
@ -140,8 +139,9 @@ IN: hashtables
>r hash>alist r> each ; inline >r hash>alist r> each ; inline
M: hashtable clone ( hash -- hash ) M: hashtable clone ( hash -- hash )
dup bucket-count dup <hashtable> [ dup bucket-count <hashtable>
hash-array rot hash-array rot copy-array over hash-size over set-hash-size [
hash-array swap hash-array dup array-capacity copy-array
] keep ; ] keep ;
: hash-subset? ( subset of -- ? ) : hash-subset? ( subset of -- ? )

View File

@ -56,7 +56,7 @@ errors unparser logging listener url-encoding hashtables memory ;
#! Write out the HTML for the list of words in a vocabulary. #! Write out the HTML for the list of words in a vocabulary.
<select name= "words" style= "width: 200" size= "20" onchange= "document.forms.main.submit()" select> <select name= "words" style= "width: 200" size= "20" onchange= "document.forms.main.submit()" select>
words [ words [
word-name dup "current-word" get [ "" ] unless* str-compare 0 = [ word-name dup "current-word" get [ "" ] unless* string-compare 0 = [
"<option selected>" write "<option selected>" write
] [ ] [
"<option>" write "<option>" write

View File

@ -73,7 +73,7 @@ USE: unparser
] ifte ; ] ifte ;
: serve-directory ( filename -- ) : serve-directory ( filename -- )
"/" ?str-tail [ "/" ?string-tail [
dup "/index.html" cat2 dup exists? [ dup "/index.html" cat2 dup exists? [
serve-file serve-file
] [ ] [

View File

@ -18,7 +18,7 @@ url-encoding presentation generic ;
: chars>entities ( str -- str ) : chars>entities ( str -- str )
#! Convert <, >, &, ' and " to HTML entities. #! Convert <, >, &, ' and " to HTML entities.
[ dup html-entities assoc dup rot ? ] str-map ; [ dup html-entities assoc dup rot ? ] string-map ;
: >hex-color ( triplet -- hex ) : >hex-color ( triplet -- hex )
[ >hex 2 "0" pad ] map "#" swons cat ; [ >hex 2 "0" pad ] map "#" swons cat ;
@ -64,8 +64,8 @@ url-encoding presentation generic ;
#! The file responder needs relative links not absolute #! The file responder needs relative links not absolute
#! links. #! links.
"doc-root" get [ "doc-root" get [
?str-head [ "/" ?str-head drop ] when ?string-head [ "/" ?string-head drop ] when
] when* "/" ?str-tail drop ; ] when* "/" ?string-tail drop ;
: file-link-href ( path -- href ) : file-link-href ( path -- href )
[ "/" , resolve-file-link url-encode , ] make-string ; [ "/" , resolve-file-link url-encode , ] make-string ;

View File

@ -13,7 +13,7 @@ stdio streams strings threads url-encoding ;
] ifte ; ] ifte ;
: (url>path) ( uri -- path ) : (url>path) ( uri -- path )
url-decode "http://" ?str-head [ url-decode "http://" ?string-head [
"/" split1 dup "" ? nip "/" split1 dup "" ? nip
] when ; ] when ;
@ -25,7 +25,7 @@ stdio streams strings threads url-encoding ;
] ifte ; ] ifte ;
: secure-path ( path -- path ) : secure-path ( path -- path )
".." over str-contains? [ drop f ] when ; ".." over string-contains? [ drop f ] when ;
: request-method ( cmd -- method ) : request-method ( cmd -- method )
[ [

View File

@ -103,13 +103,13 @@ USE: strings
: trim-/ ( url -- url ) : trim-/ ( url -- url )
#! Trim a leading /, if there is one. #! Trim a leading /, if there is one.
"/" ?str-head drop ; "/" ?string-head drop ;
: serve-responder ( method url -- ) : serve-responder ( method url -- )
#! Responder URLs come in two forms: #! Responder URLs come in two forms:
#! /foo/bar... - default-responder used #! /foo/bar... - default-responder used
#! /responder/foo/bar - responder foo, argument bar #! /responder/foo/bar - responder foo, argument bar
dup log-responder trim-/ "responder/" ?str-head [ dup log-responder trim-/ "responder/" ?string-head [
serve-explicit-responder serve-explicit-responder
] [ ] [
serve-default-responder serve-default-responder

View File

@ -40,14 +40,14 @@ USE: unparser
dup url-quotable? [ dup url-quotable? [
"%" swap >hex 2 "0" pad cat2 "%" swap >hex 2 "0" pad cat2
] unless ] unless
] str-map ; ] string-map ;
: catch-hex> ( str -- n ) : catch-hex> ( str -- n )
#! Push f if string is not a valid hex literal. #! Push f if string is not a valid hex literal.
[ hex> ] [ [ drop f ] when ] catch ; [ hex> ] [ [ drop f ] when ] catch ;
: url-decode-hex ( index str -- ) : url-decode-hex ( index str -- )
2dup str-length 2 - >= [ 2dup string-length 2 - >= [
2drop 2drop
] [ ] [
>r 1 + dup 2 + r> substring catch-hex> [ , ] when* >r 1 + dup 2 + r> substring catch-hex> [ , ] when*
@ -60,10 +60,10 @@ USE: unparser
dup CHAR: + = [ drop CHAR: \s ] when , >r 1 + r> ; dup CHAR: + = [ drop CHAR: \s ] when , >r 1 + r> ;
: url-decode-iter ( index str -- ) : url-decode-iter ( index str -- )
2dup str-length >= [ 2dup string-length >= [
2drop 2drop
] [ ] [
2dup str-nth dup CHAR: % = [ 2dup string-nth dup CHAR: % = [
drop url-decode-% drop url-decode-%
] [ ] [
url-decode-+-or-other url-decode-+-or-other

View File

@ -78,15 +78,15 @@ C: buffer ( size -- buffer )
dup buffer-size swap buffer-fill - ; dup buffer-size swap buffer-fill - ;
: buffer-set ( string buffer -- ) : buffer-set ( string buffer -- )
2dup buffer-ptr string>memory >r str-length r> buffer-reset ; 2dup buffer-ptr string>memory >r string-length r> buffer-reset ;
: (check-overflow) ( string buffer -- ) : (check-overflow) ( string buffer -- )
buffer-capacity swap str-length < [ "Buffer overflow" throw ] when ; buffer-capacity swap string-length < [ "Buffer overflow" throw ] when ;
: buffer-append ( string buffer -- ) : buffer-append ( string buffer -- )
2dup (check-overflow) 2dup (check-overflow)
[ dup buffer-ptr swap buffer-fill + string>memory ] 2keep [ dup buffer-ptr swap buffer-fill + string>memory ] 2keep
[ buffer-fill swap str-length + ] keep set-buffer-fill ; [ buffer-fill swap string-length + ] keep set-buffer-fill ;
: buffer-append-char ( int buffer -- ) : buffer-append-char ( int buffer -- )
#! Append a single character to a buffer #! Append a single character to a buffer

View File

@ -12,7 +12,7 @@ strings unparser ;
: directory ( dir -- list ) : directory ( dir -- list )
#! List a directory. #! List a directory.
(directory) [ str-lexi> ] sort ; (directory) [ string> ] sort ;
: file-length ( file -- length ) : file-length ( file -- length )
stat dup [ cdr cdr car ] when ; stat dup [ cdr cdr car ] when ;

View File

@ -46,7 +46,7 @@ BUILTIN: port 14 ;
: blocking-write ( str port -- ) : blocking-write ( str port -- )
over over
dup string? [ str-length ] [ drop 1 ] ifte dup string? [ string-length ] [ drop 1 ] ifte
over wait-to-write write-fd-8 ; over wait-to-write write-fd-8 ;
: blocking-fill ( port -- ) : blocking-fill ( port -- )
@ -56,7 +56,7 @@ BUILTIN: port 14 ;
dup can-read-line? [ drop ] [ blocking-fill ] ifte ; dup can-read-line? [ drop ] [ blocking-fill ] ifte ;
: blocking-read-line ( port -- line ) : blocking-read-line ( port -- line )
dup wait-to-read-line read-line-fd-8 dup [ sbuf>str ] when ; dup wait-to-read-line read-line-fd-8 dup [ sbuf>string ] when ;
: fill-fd ( count port -- ) : fill-fd ( count port -- )
[ add-read-count-io-task (yield) ] callcc0 2drop ; [ add-read-count-io-task (yield) ] callcc0 2drop ;
@ -65,7 +65,7 @@ BUILTIN: port 14 ;
2dup can-read-count? [ 2drop ] [ fill-fd ] ifte ; 2dup can-read-count? [ 2drop ] [ fill-fd ] ifte ;
: blocking-read ( count port -- str ) : blocking-read ( count port -- str )
2dup wait-to-read read-count-fd-8 dup [ sbuf>str ] when ; 2dup wait-to-read read-count-fd-8 dup [ sbuf>string ] when ;
: wait-to-accept ( socket -- ) : wait-to-accept ( socket -- )
[ add-accept-io-task (yield) ] callcc0 drop ; [ add-accept-io-task (yield) ] callcc0 drop ;

View File

@ -14,7 +14,7 @@ GENERIC: stream-close ( stream -- )
: stream-read1 ( stream -- char/f ) : stream-read1 ( stream -- char/f )
1 swap stream-read 1 swap stream-read
dup f-or-"" [ drop f ] [ 0 swap str-nth ] ifte ; dup f-or-"" [ drop f ] [ 0 swap string-nth ] ifte ;
: stream-write ( string stream -- ) : stream-write ( string stream -- )
f swap stream-write-attr ; f swap stream-write-attr ;
@ -37,7 +37,7 @@ M: string-output stream-auto-flush ( stream -- ) drop ;
: stream>str ( stream -- string ) : stream>str ( stream -- string )
#! Returns the string written to the given string output #! Returns the string written to the given string output
#! stream. #! stream.
string-output-buf sbuf>str ; string-output-buf sbuf>string ;
C: string-output ( size -- stream ) C: string-output ( size -- stream )
#! Creates a new stream for writing to a string buffer. #! Creates a new stream for writing to a string buffer.

View File

@ -70,11 +70,11 @@ M: integer do-write ( int -- )
buffer-append-char ; buffer-append-char ;
M: string do-write ( str -- ) M: string do-write ( str -- )
dup str-length out-buffer get buffer-capacity <= [ dup string-length out-buffer get buffer-capacity <= [
out-buffer get buffer-append out-buffer get buffer-append
] [ ] [
dup str-length out-buffer get buffer-size > [ dup string-length out-buffer get buffer-size > [
dup str-length out-buffer get buffer-extend do-write dup string-length out-buffer get buffer-extend do-write
] [ flush-output do-write ] ifte ] [ flush-output do-write ] ifte
] ifte ; ] ifte ;
@ -95,16 +95,16 @@ M: string do-write ( str -- )
dup in-buffer get buffer-first-n dup in-buffer get buffer-first-n
swap in-buffer get buffer-consume ; swap in-buffer get buffer-consume ;
: sbuf>str-or-f ( sbuf -- str-or-? ) : sbuf>string-or-f ( sbuf -- str-or-? )
dup sbuf-length 0 > [ sbuf>str ] [ drop f ] ifte ; dup sbuf-length 0 > [ sbuf>string ] [ drop f ] ifte ;
: do-read-count ( sbuf count -- str ) : do-read-count ( sbuf count -- str )
dup 0 = [ dup 0 = [
drop sbuf>str drop sbuf>string
] [ ] [
dup consume-input dup consume-input
dup str-length dup 0 = [ dup string-length dup 0 = [
3drop sbuf>str-or-f 3drop sbuf>string-or-f
] [ ] [
>r swap r> - >r swap [ sbuf-append ] keep r> do-read-count >r swap r> - >r swap [ sbuf-append ] keep r> do-read-count
] ifte ] ifte
@ -114,14 +114,14 @@ M: string do-write ( str -- )
1 in-buffer get buffer-first-n ; 1 in-buffer get buffer-first-n ;
: do-read-line ( sbuf -- str ) : do-read-line ( sbuf -- str )
1 consume-input dup str-length 0 = [ drop sbuf>str-or-f ] [ 1 consume-input dup string-length 0 = [ drop sbuf>string-or-f ] [
dup "\r" = [ dup "\r" = [
peek-input "\n" = [ 1 consume-input drop ] when peek-input "\n" = [ 1 consume-input drop ] when
drop sbuf>str drop sbuf>string
] [ ] [
dup "\n" = [ dup "\n" = [
peek-input "\r" = [ 1 consume-input drop ] when peek-input "\r" = [ 1 consume-input drop ] when
drop sbuf>str drop sbuf>string
] [ ] [
over sbuf-append do-read-line over sbuf-append do-read-line
] ifte ] ifte

View File

@ -8,25 +8,25 @@ IN: strings USING: kernel lists math namespaces strings ;
[ swap [ dup , ] times drop ] make-string ; [ swap [ dup , ] times drop ] make-string ;
: pad ( string count char -- string ) : pad ( string count char -- string )
>r over str-length - dup 0 <= [ >r over string-length - dup 0 <= [
r> 2drop r> 2drop
] [ ] [
r> fill swap cat2 r> fill swap cat2
] ifte ; ] ifte ;
: str-map ( str code -- str ) : string-map ( str code -- str )
#! Apply a quotation to each character in the string, and #! Apply a quotation to each character in the string, and
#! push a new string constructed from return values. #! push a new string constructed from return values.
#! The quotation must have stack effect ( X -- X ). #! The quotation must have stack effect ( X -- X ).
over str-length <sbuf> rot [ over string-length <sbuf> rot [
swap >r apply swap r> tuck sbuf-append swap >r apply swap r> tuck sbuf-append
] str-each nip sbuf>str ; inline ] string-each nip sbuf>string ; inline
: split-next ( index string split -- next ) : split-next ( index string split -- next )
3dup index-of* dup -1 = [ 3dup index-of* dup -1 = [
>r drop str-tail , r> ( end of string ) >r drop string-tail , r> ( end of string )
] [ ] [
swap str-length dupd + >r swap substring , r> swap string-length dupd + >r swap substring , r>
] ifte ; ] ifte ;
: (split) ( index string split -- ) : (split) ( index string split -- )
@ -42,10 +42,10 @@ IN: strings USING: kernel lists math namespaces strings ;
[ 0 -rot (split) ] make-list ; [ 0 -rot (split) ] make-list ;
: split-n-advance substring , >r tuck + swap r> ; : split-n-advance substring , >r tuck + swap r> ;
: split-n-finish nip dup str-length swap substring , ; : split-n-finish nip dup string-length swap substring , ;
: (split-n) ( start n str -- ) : (split-n) ( start n str -- )
3dup >r dupd + r> 2dup str-length < [ 3dup >r dupd + r> 2dup string-length < [
split-n-advance (split-n) split-n-advance (split-n)
] [ ] [
split-n-finish 3drop split-n-finish 3drop
@ -55,5 +55,5 @@ IN: strings USING: kernel lists math namespaces strings ;
#! Split a string into n-character chunks. #! Split a string into n-character chunks.
[ 0 -rot (split-n) ] make-list ; [ 0 -rot (split-n) ] make-list ;
: ch>str ( ch -- str ) : ch>string ( ch -- str )
1 <sbuf> [ sbuf-append ] keep sbuf>str ; 1 <sbuf> [ sbuf-append ] keep sbuf>string ;

View File

@ -135,12 +135,12 @@ global [
] when drop ; ] when drop ;
: filter-nulls ( str -- str ) : filter-nulls ( str -- str )
"\0" over str-contains? [ "\0" over string-contains? [
[ dup CHAR: \0 = [ drop CHAR: \s ] when ] str-map [ dup CHAR: \0 = [ drop CHAR: \s ] when ] string-map
] when ; ] when ;
: draw-string ( x y font text fg -- width ) : draw-string ( x y font text fg -- width )
>r filter-nulls r> over str-length 0 = [ >r filter-nulls r> over string-length 0 = [
2drop 3drop 0 2drop 3drop 0
] [ ] [
>r >r lookup-font r> r> >r >r lookup-font r> r>
@ -151,7 +151,7 @@ global [
] ifte ; ] ifte ;
: size-string ( font text -- w h ) : size-string ( font text -- w h )
>r lookup-font r> filter-nulls dup str-length 0 = [ >r lookup-font r> filter-nulls dup string-length 0 = [
drop TTF_FontHeight 0 swap drop TTF_FontHeight 0 swap
] [ ] [
<int-box> <int-box> [ TTF_SizeUNICODE drop ] 2keep <int-box> <int-box> [ TTF_SizeUNICODE drop ] 2keep

View File

@ -2,8 +2,8 @@
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: strings USING: generic kernel kernel-internals lists math ; IN: strings USING: generic kernel kernel-internals lists math ;
BUILTIN: string 12 [ 1 "str-length" f ] [ 2 hashcode f ] ; BUILTIN: string 12 [ 1 "string-length" f ] [ 2 hashcode f ] ;
M: string = str= ; M: string = string= ;
BUILTIN: sbuf 13 ; BUILTIN: sbuf 13 ;
M: sbuf = sbuf= ; M: sbuf = sbuf= ;
@ -13,21 +13,21 @@ UNION: text string integer ;
: f-or-"" ( obj -- ? ) : f-or-"" ( obj -- ? )
dup not swap "" = or ; dup not swap "" = or ;
: str-length< ( str str -- boolean ) : string-length< ( str str -- boolean )
#! Compare string lengths. #! Compare string lengths.
swap str-length swap str-length < ; swap string-length swap string-length < ;
: cat ( [ "a" "b" "c" ] -- "abc" ) : cat ( [ "a" "b" "c" ] -- "abc" )
! If f appears in the list, it is not appended to the ! If f appears in the list, it is not appended to the
! string. ! string.
80 <sbuf> swap [ [ over sbuf-append ] when* ] each sbuf>str ; 80 <sbuf> swap [ [ over sbuf-append ] when* ] each sbuf>string ;
: cat2 ( "a" "b" -- "ab" ) : cat2 ( "a" "b" -- "ab" )
swap swap
80 <sbuf> 80 <sbuf>
dup >r sbuf-append r> dup >r sbuf-append r>
dup >r sbuf-append r> dup >r sbuf-append r>
sbuf>str ; sbuf>string ;
: cat3 ( "a" "b" "c" -- "abc" ) : cat3 ( "a" "b" "c" -- "abc" )
[ ] cons cons cons cat ; [ ] cons cons cons cat ;
@ -35,58 +35,58 @@ UNION: text string integer ;
: index-of ( string substring -- index ) : index-of ( string substring -- index )
0 -rot index-of* ; 0 -rot index-of* ;
: str-lexi> ( str1 str2 -- ? ) : string> ( str1 str2 -- ? )
! Returns if the first string lexicographically follows str2 ! Returns if the first string lexicographically follows str2
str-compare 0 > ; string-compare 0 > ;
: str-head ( index str -- str ) : string-head ( index str -- str )
#! Returns a new string, from the beginning of the string #! Returns a new string, from the beginning of the string
#! until the given index. #! until the given index.
0 -rot substring ; 0 -rot substring ;
: str-contains? ( substr str -- ? ) : string-contains? ( substr str -- ? )
swap index-of -1 = not ; swap index-of -1 = not ;
: str-tail ( index str -- str ) : string-tail ( index str -- str )
#! Returns a new string, from the given index until the end #! Returns a new string, from the given index until the end
#! of the string. #! of the string.
[ str-length ] keep substring ; [ string-length ] keep substring ;
: str/ ( str index -- str str ) : string/ ( str index -- str str )
#! Returns 2 strings, that when concatenated yield the #! Returns 2 strings, that when concatenated yield the
#! original string. #! original string.
[ swap str-head ] 2keep swap str-tail ; [ swap string-head ] 2keep swap string-tail ;
: str// ( str index -- str str ) : string// ( str index -- str str )
#! Returns 2 strings, that when concatenated yield the #! Returns 2 strings, that when concatenated yield the
#! original string, without the character at the given #! original string, without the character at the given
#! index. #! index.
[ swap str-head ] 2keep 1 + swap str-tail ; [ swap string-head ] 2keep 1 + swap string-tail ;
: str-head? ( str begin -- ? ) : string-head? ( str begin -- ? )
2dup str-length< [ 2dup string-length< [
2drop f 2drop f
] [ ] [
dup str-length rot str-head = dup string-length rot string-head =
] ifte ; ] ifte ;
: ?str-head ( str begin -- str ? ) : ?string-head ( str begin -- str ? )
2dup str-head? [ 2dup string-head? [
str-length swap str-tail t string-length swap string-tail t
] [ ] [
drop f drop f
] ifte ; ] ifte ;
: str-tail? ( str end -- ? ) : string-tail? ( str end -- ? )
2dup str-length< [ 2dup string-length< [
2drop f 2drop f
] [ ] [
dup str-length pick str-length swap - rot str-tail = dup string-length pick string-length swap - rot string-tail =
] ifte ; ] ifte ;
: ?str-tail ( str end -- ? ) : ?string-tail ( str end -- ? )
2dup str-tail? [ 2dup string-tail? [
str-length swap [ str-length swap - ] keep str-head t string-length swap [ string-length swap - ] keep string-head t
] [ ] [
drop f drop f
] ifte ; ] ifte ;
@ -95,26 +95,26 @@ UNION: text string integer ;
2dup index-of dup -1 = [ 2dup index-of dup -1 = [
2drop f 2drop f
] [ ] [
[ swap str-length + over str-tail ] keep [ swap string-length + over string-tail ] keep
rot str-head swap rot string-head swap
] ifte ; ] ifte ;
: (str>list) ( i str -- list ) : (string>list) ( i str -- list )
2dup str-length >= [ 2dup string-length >= [
2drop [ ] 2drop [ ]
] [ ] [
2dup str-nth >r >r 1 + r> (str>list) r> swons 2dup string-nth >r >r 1 + r> (string>list) r> swons
] ifte ; ] ifte ;
: str>list ( str -- list ) : string>list ( str -- list )
0 swap (str>list) ; 0 swap (string>list) ;
: str-each ( str quot -- ) : string-each ( str quot -- )
#! Execute the quotation with each character of the string #! Execute the quotation with each character of the string
#! pushed onto the stack. #! pushed onto the stack.
>r str>list r> each ; inline >r string>list r> each ; inline
PREDICATE: integer blank " \t\n\r" str-contains? ; PREDICATE: integer blank " \t\n\r" string-contains? ;
PREDICATE: integer letter CHAR: a CHAR: z between? ; PREDICATE: integer letter CHAR: a CHAR: z between? ;
PREDICATE: integer LETTER CHAR: A CHAR: Z between? ; PREDICATE: integer LETTER CHAR: A CHAR: Z between? ;
PREDICATE: integer digit CHAR: 0 CHAR: 9 between? ; PREDICATE: integer digit CHAR: 0 CHAR: 9 between? ;
@ -123,7 +123,7 @@ PREDICATE: integer printable CHAR: \s CHAR: ~ between? ;
: quotable? ( ch -- ? ) : quotable? ( ch -- ? )
#! In a string literal, can this character be used without #! In a string literal, can this character be used without
#! escaping? #! escaping?
dup printable? swap "\"\\" str-contains? not and ; dup printable? swap "\"\\" string-contains? not and ;
: url-quotable? ( ch -- ? ) : url-quotable? ( ch -- ? )
#! In a URL, can this character be used without #! In a URL, can this character be used without
@ -131,4 +131,4 @@ PREDICATE: integer printable CHAR: \s CHAR: ~ between? ;
dup letter? dup letter?
over LETTER? or over LETTER? or
over digit? or over digit? or
swap "/_?." str-contains? or ; swap "/_?." string-contains? or ;

View File

@ -50,26 +50,26 @@ M: object digit> not-a-number ;
2dup < [ rot * + ] [ not-a-number ] ifte ; 2dup < [ rot * + ] [ not-a-number ] ifte ;
: (base>) ( base str -- num ) : (base>) ( base str -- num )
dup str-length 0 = [ dup string-length 0 = [
not-a-number not-a-number
] [ ] [
0 swap [ digit> pick digit+ ] str-each nip 0 swap [ digit> pick digit+ ] string-each nip
] ifte ; ] ifte ;
: base> ( str base -- num ) : base> ( str base -- num )
#! Convert a string to an integer. Throw an error if #! Convert a string to an integer. Throw an error if
#! conversion fails. #! conversion fails.
swap "-" ?str-head [ (base>) neg ] [ (base>) ] ifte ; swap "-" ?string-head [ (base>) neg ] [ (base>) ] ifte ;
GENERIC: str>number ( str -- num ) GENERIC: str>number ( str -- num )
M: string str>number 10 base> ; M: string str>number 10 base> ;
PREDICATE: string potential-ratio "/" swap str-contains? ; PREDICATE: string potential-ratio "/" swap string-contains? ;
M: potential-ratio str>number ( str -- num ) M: potential-ratio str>number ( str -- num )
dup CHAR: / index-of str// swap 10 base> swap 10 base> / ; dup CHAR: / index-of string// swap 10 base> swap 10 base> / ;
PREDICATE: string potential-float "." swap str-contains? ; PREDICATE: string potential-float "." swap string-contains? ;
M: potential-float str>number ( str -- num ) M: potential-float str>number ( str -- num )
str>float ; str>float ;

View File

@ -96,7 +96,7 @@ math namespaces parser strings words vectors unparse ;
! String literal ! String literal
: parse-string ( n str -- n ) : parse-string ( n str -- n )
2dup str-nth CHAR: " = [ 2dup string-nth CHAR: " = [
drop 1 + drop 1 +
] [ ] [
[ next-char swap , ] keep parse-string [ next-char swap , ] keep parse-string

View File

@ -20,14 +20,14 @@ unparser ;
: skip ( n line quot -- n ) : skip ( n line quot -- n )
#! Find the next character that satisfies the quotation, #! Find the next character that satisfies the quotation,
#! which should have stack effect ( ch -- ? ). #! which should have stack effect ( ch -- ? ).
>r 2dup str-length < [ >r 2dup string-length < [
2dup str-nth r> dup >r call [ 2dup string-nth r> dup >r call [
r> 2drop r> 2drop
] [ ] [
>r 1 + r> r> skip >r 1 + r> r> skip
] ifte ] ifte
] [ ] [
r> drop nip str-length r> drop nip string-length
] ifte ; inline ] ifte ; inline
: skip-blank ( n line -- n ) : skip-blank ( n line -- n )
@ -41,10 +41,10 @@ unparser ;
#! "hello world" #! "hello world"
#! #!
#! Will call the parsing word ". #! Will call the parsing word ".
"\"" str-contains? ; "\"" string-contains? ;
: skip-word ( n line -- n ) : skip-word ( n line -- n )
2dup str-nth denotation? [ 2dup string-nth denotation? [
drop 1 + drop 1 +
] [ ] [
[ blank? ] skip [ blank? ] skip
@ -52,7 +52,7 @@ unparser ;
: (scan) ( n line -- start end ) : (scan) ( n line -- start end )
[ skip-blank dup ] keep [ skip-blank dup ] keep
2dup str-length < [ skip-word ] [ drop ] ifte ; 2dup string-length < [ skip-word ] [ drop ] ifte ;
: scan ( -- token ) : scan ( -- token )
"col" get "line" get dup >r (scan) dup "col" set "col" get "line" get dup >r (scan) dup "col" set
@ -98,7 +98,7 @@ global [ string-mode off ] bind
ch-search (until) ; ch-search (until) ;
: (until-eol) ( -- index ) : (until-eol) ( -- index )
"\n" ch-search dup -1 = [ drop "line" get str-length ] when ; "\n" ch-search dup -1 = [ drop "line" get string-length ] when ;
: until-eol ( -- str ) : until-eol ( -- str )
#! This is just a hack to get "eval" to work with multiline #! This is just a hack to get "eval" to work with multiline
@ -132,17 +132,17 @@ global [ string-mode off ] bind
] assoc dup [ "Bad escape" throw ] unless ; ] assoc dup [ "Bad escape" throw ] unless ;
: next-escape ( n str -- ch n ) : next-escape ( n str -- ch n )
2dup str-nth CHAR: u = [ 2dup string-nth CHAR: u = [
swap 1 + dup 4 + [ rot substring hex> ] keep swap 1 + dup 4 + [ rot substring hex> ] keep
] [ ] [
over 1 + >r str-nth escape r> over 1 + >r string-nth escape r>
] ifte ; ] ifte ;
: next-char ( n str -- ch n ) : next-char ( n str -- ch n )
2dup str-nth CHAR: \\ = [ 2dup string-nth CHAR: \\ = [
>r 1 + r> next-escape >r 1 + r> next-escape
] [ ] [
over 1 + >r str-nth r> over 1 + >r string-nth r>
] ifte ; ] ifte ;
: doc-comment-here? ( parsed -- ? ) : doc-comment-here? ( parsed -- ? )

View File

@ -54,7 +54,7 @@ M: ratio unparse ( num -- str )
: fix-float ( str -- str ) : fix-float ( str -- str )
#! This is terrible. Will go away when we do our own float #! This is terrible. Will go away when we do our own float
#! output. #! output.
"." over str-contains? [ ".0" cat2 ] unless ; "." over string-contains? [ ".0" cat2 ] unless ;
M: float unparse ( float -- str ) M: float unparse ( float -- str )
(unparse-float) fix-float ; (unparse-float) fix-float ;
@ -90,7 +90,7 @@ M: complex unparse ( num -- str )
M: string unparse ( str -- str ) M: string unparse ( str -- str )
[ [
CHAR: " , [ unparse-ch , ] str-each CHAR: " , CHAR: " , [ unparse-ch , ] string-each CHAR: " ,
] make-string ; ] make-string ;
M: word unparse ( obj -- str ) M: word unparse ( obj -- str )

View File

@ -9,10 +9,10 @@ USE: compiler
! http://inferno.bell-labs.com/cm/cs/who/bwk/interps/pap.html ! http://inferno.bell-labs.com/cm/cs/who/bwk/interps/pap.html
: string-step ( n str -- ) : string-step ( n str -- )
2dup str-length > [ 2dup string-length > [
dup [ "123" , , "456" , , "789" , ] make-string dup [ "123" , , "456" , , "789" , ] make-string
dup dup str-length 2 /i 0 swap rot substring dup dup string-length 2 /i 0 swap rot substring
swap dup str-length 2 /i 1 + 1 swap rot substring cat2 swap dup string-length 2 /i 1 + 1 swap rot substring cat2
string-step string-step
] [ ] [
2drop 2drop

View File

@ -53,7 +53,7 @@ prettyprint strings test vectors words ;
! Forgot to tag out of bounds index ! Forgot to tag out of bounds index
[ 1 { } vector-nth ] [ garbage-collection drop ] catch [ 1 { } vector-nth ] [ garbage-collection drop ] catch
[ -1 { } set-vector-length ] [ garbage-collection drop ] catch [ -1 { } set-vector-length ] [ garbage-collection drop ] catch
[ 1 "" str-nth ] [ garbage-collection drop ] catch [ 1 "" string-nth ] [ garbage-collection drop ] catch
! ... and again ! ... and again
[ "" 10 str/ ] [ . ] catch [ "" 10 string/ ] [ . ] catch

View File

@ -84,3 +84,38 @@ f 100000000000000000000000000 "testhash" get set-hash
[ f ] [ {{ }} {{ [[ 1 3 ]] }} = ] unit-test [ f ] [ {{ }} {{ [[ 1 3 ]] }} = ] unit-test
[ t ] [ {{ [[ 1 3 ]] }} {{ [[ 1 3 ]] }} = ] unit-test [ t ] [ {{ [[ 1 3 ]] }} {{ [[ 1 3 ]] }} = ] unit-test
[ f ] [ {{ [[ 1 3 ]] }} {{ [[ 1 "hey" ]] }} = ] unit-test [ f ] [ {{ [[ 1 3 ]] }} {{ [[ 1 "hey" ]] }} = ] unit-test
! Test rehashing
2 <hashtable> "rehash" set
1 1 "rehash" get set-hash
2 2 "rehash" get set-hash
3 3 "rehash" get set-hash
4 4 "rehash" get set-hash
5 5 "rehash" get set-hash
6 6 "rehash" get set-hash
[ 6 ] [ "rehash" get hash-size ] unit-test
[ 6 ] [ "rehash" get clone hash-size ] unit-test
"rehash" get hash-clear
[ 0 ] [ "rehash" get hash-size ] unit-test
[
3
] [
2 {{
[[ 1 2 ]]
[[ 2 3 ]]
}} clone hash
] unit-test
! There was an assoc in place of assoc* somewhere
3 <hashtable> "f-hash-test" set
10 [ f f "f-hash-test" get set-hash ] times
[ 1 ] [ "f-hash-test" get hash-size ] unit-test

View File

@ -15,7 +15,7 @@ USE: prettyprint
[ t ] [ [ t ] [
"editor" get [ caret get ] bind "editor" get [ caret get ] bind
"Hello world" str-length = "Hello world" string-length =
] unit-test ] unit-test
[ "Hello, crazy world" ] [ [ "Hello, crazy world" ] [
@ -33,7 +33,7 @@ USE: prettyprint
] unit-test ] unit-test
[ "Hello, crazy" ] [ [ "Hello, crazy" ] [
"editor" get [ caret get line-text get str-head ] bind "editor" get [ caret get line-text get string-head ] bind
] unit-test ] unit-test
[ 0 ] [ 0 ]

View File

@ -19,7 +19,7 @@ USE: strings
[ "fdsfs" [ > ] sort ] unit-test-fails [ "fdsfs" [ > ] sort ] unit-test-fails
[ [ ] ] [ [ ] [ > ] sort ] unit-test [ [ ] ] [ [ ] [ > ] sort ] unit-test
[ [ "2 + 2" ] ] [ [ "2 + 2" ] [ str-lexi> ] sort ] unit-test [ [ "2 + 2" ] ] [ [ "2 + 2" ] [ string> ] sort ] unit-test
[ [ 1 2 3 4 5 6 7 ] ] [ [ 6 4 5 7 2 1 3 ] [ > ] sort ] unit-test [ [ 1 2 3 4 5 6 7 ] ] [ [ 6 4 5 7 2 1 3 ] [ > ] sort ] unit-test
[ f ] [ [ { } { } "Hello" ] all=? ] unit-test [ f ] [ [ { } { } "Hello" ] all=? ] unit-test

View File

@ -19,5 +19,5 @@ DEFER: foo
! Test > 1 ( ) comment; only the first one should be used. ! Test > 1 ( ) comment; only the first one should be used.
[ t ] [ [ t ] [
"a" ": foo ( a ) ( b ) ;" parse drop word "a" ": foo ( a ) ( b ) ;" parse drop word
"stack-effect" word-prop str-contains? "stack-effect" word-prop string-contains?
] unit-test ] unit-test

View File

@ -11,5 +11,5 @@ USE: test
"Hello" "buf" get sbuf-append "Hello" "buf" get sbuf-append
"buf" get sbuf-clone "buf-clone" set "buf" get sbuf-clone "buf-clone" set
"World" "buf-clone" get sbuf-append "World" "buf-clone" get sbuf-append
"buf" get sbuf>str "buf" get sbuf>string
] unit-test ] unit-test

View File

@ -24,29 +24,29 @@ USE: test
[ -1 ] [ "hola" "amigo" index-of ] unit-test [ -1 ] [ "hola" "amigo" index-of ] unit-test
[ -1 ] [ "hola" "holaa" index-of ] unit-test [ -1 ] [ "hola" "holaa" index-of ] unit-test
[ "Beginning" ] [ 9 "Beginning and end" str-head ] unit-test [ "Beginning" ] [ 9 "Beginning and end" string-head ] unit-test
[ f ] [ "I" "team" str-contains? ] unit-test [ f ] [ "I" "team" string-contains? ] unit-test
[ t ] [ "ea" "team" str-contains? ] unit-test [ t ] [ "ea" "team" string-contains? ] unit-test
[ f ] [ "actore" "Factor" str-contains? ] unit-test [ f ] [ "actore" "Factor" string-contains? ] unit-test
[ "end" ] [ 14 "Beginning and end" str-tail ] unit-test [ "end" ] [ 14 "Beginning and end" string-tail ] unit-test
[ "Beginning" " and end" ] [ "Beginning and end" 9 str/ ] unit-test [ "Beginning" " and end" ] [ "Beginning and end" 9 string/ ] unit-test
[ "Beginning" "and end" ] [ "Beginning and end" 9 str// ] unit-test [ "Beginning" "and end" ] [ "Beginning and end" 9 string// ] unit-test
[ "hello" "world" ] [ "hello world" " " split1 ] unit-test [ "hello" "world" ] [ "hello world" " " split1 ] unit-test
[ "goodbye" f ] [ "goodbye" " " split1 ] unit-test [ "goodbye" f ] [ "goodbye" " " split1 ] unit-test
[ "" "" ] [ "great" "great" split1 ] unit-test [ "" "" ] [ "great" "great" split1 ] unit-test
[ "and end" t ] [ "Beginning and end" "Beginning " ?str-head ] unit-test [ "and end" t ] [ "Beginning and end" "Beginning " ?string-head ] unit-test
[ "Beginning and end" f ] [ "Beginning and end" "Beginning x" ?str-head ] unit-test [ "Beginning and end" f ] [ "Beginning and end" "Beginning x" ?string-head ] unit-test
[ "Beginning and end" f ] [ "Beginning and end" "eginning " ?str-head ] unit-test [ "Beginning and end" f ] [ "Beginning and end" "eginning " ?string-head ] unit-test
[ "Beginning" t ] [ "Beginning and end" " and end" ?str-tail ] unit-test [ "Beginning" t ] [ "Beginning and end" " and end" ?string-tail ] unit-test
[ "Beginning and end" f ] [ "Beginning and end" "Beginning x" ?str-tail ] unit-test [ "Beginning and end" f ] [ "Beginning and end" "Beginning x" ?string-tail ] unit-test
[ "Beginning and end" f ] [ "Beginning and end" "eginning " ?str-tail ] unit-test [ "Beginning and end" f ] [ "Beginning and end" "eginning " ?string-tail ] unit-test
[ [ "This" "is" "a" "split" "sentence" ] ] [ [ "This" "is" "a" "split" "sentence" ] ]
[ "This is a split sentence" " " split ] [ "This is a split sentence" " " split ]
@ -59,10 +59,10 @@ unit-test
[ [ "a" "b" "c" "d" "e" "f" ] ] [ [ "a" "b" "c" "d" "e" "f" ] ]
[ "aXXbXXcXXdXXeXXf" "XX" split ] unit-test [ "aXXbXXcXXdXXeXXf" "XX" split ] unit-test
[ "Hello world" t ] [ "Hello world\n" "\n" ?str-tail ] unit-test [ "Hello world" t ] [ "Hello world\n" "\n" ?string-tail ] unit-test
[ "Hello world" f ] [ "Hello world" "\n" ?str-tail ] unit-test [ "Hello world" f ] [ "Hello world" "\n" ?string-tail ] unit-test
[ "" t ] [ "\n" "\n" ?str-tail ] unit-test [ "" t ] [ "\n" "\n" ?string-tail ] unit-test
[ "" f ] [ "" "\n" ?str-tail ] unit-test [ "" f ] [ "" "\n" ?string-tail ] unit-test
[ t ] [ CHAR: a letter? ] unit-test [ t ] [ CHAR: a letter? ] unit-test
[ f ] [ CHAR: A letter? ] unit-test [ f ] [ CHAR: A letter? ] unit-test
@ -71,11 +71,8 @@ unit-test
[ t ] [ CHAR: 0 digit? ] unit-test [ t ] [ CHAR: 0 digit? ] unit-test
[ f ] [ CHAR: x digit? ] unit-test [ f ] [ CHAR: x digit? ] unit-test
[ t ] [ "abc" "abd" str-compare 0 < ] unit-test [ t ] [ "abc" "abd" string-compare 0 < ] unit-test
[ t ] [ "z" "abd" str-compare 0 > ] unit-test [ t ] [ "z" "abd" string-compare 0 > ] unit-test
[ "fedcba" ] [ "abcdef" str-reverse ] unit-test
[ "edcba" ] [ "abcde" str-reverse ] unit-test
[ f ] [ [ 0 10 "hello" substring ] [ not ] catch ] unit-test [ f ] [ [ 0 10 "hello" substring ] [ not ] catch ] unit-test
@ -83,13 +80,13 @@ unit-test
[ 4 ] [ [ 4 ] [
0 "There are Four Upper Case characters" 0 "There are Four Upper Case characters"
[ LETTER? [ 1 + ] when ] str-each [ LETTER? [ 1 + ] when ] string-each
] unit-test ] unit-test
[ "Replacing+spaces+with+plus" ] [ "Replacing+spaces+with+plus" ]
[ [
"Replacing spaces with plus" "Replacing spaces with plus"
[ dup CHAR: \s = [ drop CHAR: + ] when ] str-map [ dup CHAR: \s = [ drop CHAR: + ] when ] string-map
] ]
unit-test unit-test

View File

@ -49,7 +49,7 @@ USE: listener
! captured with with-string. ! captured with with-string.
: write-packet ( string -- ) : write-packet ( string -- )
dup str-length write-big-endian-32 write flush ; dup string-length write-big-endian-32 write flush ;
: read-packet ( -- string ) : read-packet ( -- string )
read-big-endian-32 read ; read-big-endian-32 read ;
@ -75,7 +75,7 @@ USE: listener
: jedit-write-attr ( str style -- ) : jedit-write-attr ( str style -- )
CHAR: w write CHAR: w write
[ swap . . ] with-string [ swap . . ] with-string
dup str-length write-big-endian-32 dup string-length write-big-endian-32
write ; write ;
TUPLE: jedit-stream delegate ; TUPLE: jedit-stream delegate ;

View File

@ -26,7 +26,7 @@ strings unparser words ;
: send-jedit-request ( request -- ) : send-jedit-request ( request -- )
jedit-server-info swap "localhost" swap <client> [ jedit-server-info swap "localhost" swap <client> [
write-big-endian-32 write-big-endian-32
dup str-length write-big-endian-16 dup string-length write-big-endian-16
write flush write flush
] with-stream ; ] with-stream ;

View File

@ -42,7 +42,7 @@ M: 2generic word-uses? ( of in -- ? ) generic-uses? ;
: vocab-apropos ( substring vocab -- list ) : vocab-apropos ( substring vocab -- list )
#! Push a list of all words in a vocabulary whose names #! Push a list of all words in a vocabulary whose names
#! contain a string. #! contain a string.
words [ word-name dupd str-contains? ] subset nip ; words [ word-name dupd string-contains? ] subset nip ;
: vocab-apropos. ( substring vocab -- ) : vocab-apropos. ( substring vocab -- )
#! List all words in a vocabulary that contain a string. #! List all words in a vocabulary that contain a string.
@ -55,7 +55,7 @@ M: 2generic word-uses? ( of in -- ? ) generic-uses? ;
: vocab-completions ( substring vocab -- list ) : vocab-completions ( substring vocab -- list )
#! Used by jEdit plugin. Like vocab-apropos, but only #! Used by jEdit plugin. Like vocab-apropos, but only
#! matches at the start of a word name are considered. #! matches at the start of a word name are considered.
words [ word-name over ?str-head nip ] subset nip ; words [ word-name over ?string-head nip ] subset nip ;
: apropos. ( substring -- ) : apropos. ( substring -- )
#! List all words that contain a string. #! List all words that contain a string.
@ -78,7 +78,7 @@ M: 2generic word-uses? ( of in -- ? ) generic-uses? ;
: word-file ( word -- file ) : word-file ( word -- file )
"file" word-prop dup [ "file" word-prop dup [
"resource:/" ?str-head [ "resource:/" ?string-head [
resource-path swap path+ resource-path swap path+
] when ] when
] when ; ] when ;

View File

@ -28,8 +28,8 @@ TUPLE: editor line caret delegate ;
: run-char-widths ( str -- wlist ) : run-char-widths ( str -- wlist )
#! List of x co-ordinates of each character. #! List of x co-ordinates of each character.
0 swap str>list 0 swap string>list
[ ch>str shape-w [ + dup ] keep 2 /i - ] map nip ; [ ch>string shape-w [ + dup ] keep 2 /i - ] map nip ;
: (x>offset) ( n x wlist -- offset ) : (x>offset) ( n x wlist -- offset )
dup [ dup [
@ -77,7 +77,7 @@ C: editor ( text -- )
dup editor-actions ; dup editor-actions ;
: offset>x ( offset str -- x ) : offset>x ( offset str -- x )
str-head font get swap size-string drop ; string-head font get swap size-string drop ;
: caret-pos ( editor -- x y ) : caret-pos ( editor -- x y )
editor-line [ caret get line-text get ] bind offset>x 0 ; editor-line [ caret get line-text get ] bind offset>x 0 ;

View File

@ -11,7 +11,7 @@ lists namespaces strings unparser vectors words ;
: sort-sheet ( assoc -- assoc ) : sort-sheet ( assoc -- assoc )
#! Sort an association list whose keys are arbitrary objects #! Sort an association list whose keys are arbitrary objects
[ 2car swap unparse* swap unparse* str-lexi> ] sort ; [ 2car swap unparse* swap unparse* string> ] sort ;
: alist>sheet ( assoc -- sheet ) : alist>sheet ( assoc -- sheet )
unzip swap unzip swap

View File

@ -60,7 +60,7 @@ SYMBOL: history-index
: set-line-text ( text -- ) : set-line-text ( text -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
dup line-text set str-length caret set ; dup line-text set string-length caret set ;
: goto-history ( n -- ) : goto-history ( n -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
@ -99,7 +99,7 @@ SYMBOL: history-index
: caret-insert ( str offset -- ) : caret-insert ( str offset -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
caret get <= [ caret get <= [
str-length caret [ + ] change string-length caret [ + ] change
] [ ] [
drop drop
] ifte ; ] ifte ;
@ -108,12 +108,12 @@ SYMBOL: history-index
#! Call this in the line editor scope. #! Call this in the line editor scope.
reset-history reset-history
2dup caret-insert 2dup caret-insert
line-text get swap str/ line-text get swap string/
swapd cat3 line-text set ; swapd cat3 line-text set ;
: insert-char ( ch -- ) : insert-char ( ch -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
ch>str caret get line-insert ; ch>string caret get line-insert ;
: caret-remove ( offset length -- ) : caret-remove ( offset length -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
@ -131,8 +131,8 @@ SYMBOL: history-index
#! Call this in the line editor scope. #! Call this in the line editor scope.
reset-history reset-history
2dup caret-remove 2dup caret-remove
dupd + line-text get str-tail dupd + line-text get string-tail
>r line-text get str-head r> cat2 >r line-text get string-head r> cat2
line-text set ; line-text set ;
: backspace ( -- ) : backspace ( -- )
@ -145,4 +145,4 @@ SYMBOL: history-index
: right ( -- ) : right ( -- )
#! Call this in the line editor scope. #! Call this in the line editor scope.
caret [ 1 + line-text get str-length min ] change ; caret [ 1 + line-text get string-length min ] change ;

View File

@ -7,7 +7,7 @@ IN: words USING: hashtables kernel lists namespaces strings ;
: vocabs ( -- list ) : vocabs ( -- list )
#! Push a list of vocabularies. #! Push a list of vocabularies.
vocabularies get hash-keys [ str-lexi> ] sort ; vocabularies get hash-keys [ string> ] sort ;
: vocab ( name -- vocab ) : vocab ( name -- vocab )
#! Get a vocabulary. #! Get a vocabulary.
@ -15,7 +15,7 @@ IN: words USING: hashtables kernel lists namespaces strings ;
: word-sort ( list -- list ) : word-sort ( list -- list )
#! Sort a list of words by name. #! Sort a list of words by name.
[ swap word-name swap word-name str-lexi> ] sort ; [ swap word-name swap word-name string> ] sort ;
: words ( vocab -- list ) : words ( vocab -- list )
#! Push a list of all words in a vocabulary. #! Push a list of all words in a vocabulary.

View File

@ -14,7 +14,6 @@ void* primitives[] = {
primitive_string_eq, primitive_string_eq,
primitive_index_of, primitive_index_of,
primitive_substring, primitive_substring,
primitive_string_reverse,
primitive_sbuf, primitive_sbuf,
primitive_sbuf_length, primitive_sbuf_length,
primitive_set_sbuf_length, primitive_set_sbuf_length,
@ -22,7 +21,6 @@ void* primitives[] = {
primitive_set_sbuf_nth, primitive_set_sbuf_nth,
primitive_sbuf_append, primitive_sbuf_append,
primitive_sbuf_to_string, primitive_sbuf_to_string,
primitive_sbuf_reverse,
primitive_sbuf_clone, primitive_sbuf_clone,
primitive_sbuf_eq, primitive_sbuf_eq,
primitive_arithmetic_type, primitive_arithmetic_type,

View File

@ -133,12 +133,6 @@ void primitive_sbuf_to_string(void)
drepl(tag_object(s)); drepl(tag_object(s));
} }
void primitive_sbuf_reverse(void)
{
F_SBUF* sbuf = untag_sbuf(dpop());
string_reverse(untag_string(sbuf->string),sbuf->top);
}
void primitive_sbuf_clone(void) void primitive_sbuf_clone(void)
{ {
F_SBUF* s; F_SBUF* s;

View File

@ -25,7 +25,6 @@ void primitive_set_sbuf_nth(void);
void sbuf_append_string(F_SBUF* sbuf, F_STRING* string); void sbuf_append_string(F_SBUF* sbuf, F_STRING* string);
void primitive_sbuf_append(void); void primitive_sbuf_append(void);
void primitive_sbuf_to_string(void); void primitive_sbuf_to_string(void);
void primitive_sbuf_reverse(void);
void primitive_sbuf_clone(void); void primitive_sbuf_clone(void);
bool sbuf_eq(F_SBUF* s1, F_SBUF* s2); bool sbuf_eq(F_SBUF* s1, F_SBUF* s2);
void primitive_sbuf_eq(void); void primitive_sbuf_eq(void);

View File

@ -312,21 +312,6 @@ void primitive_substring(void)
dpush(tag_object(substring(start,end,string))); dpush(tag_object(substring(start,end,string)));
} }
/* DESTRUCTIVE - don't use with user-visible strings */
void string_reverse(F_STRING* s, int len)
{
int i, j;
uint16_t ch1, ch2;
for(i = 0; i < len / 2; i++)
{
j = len - i - 1;
ch1 = string_nth(s,i);
ch2 = string_nth(s,j);
set_string_nth(s,j,ch1);
set_string_nth(s,i,ch2);
}
}
/* Doesn't rehash the string! */ /* Doesn't rehash the string! */
F_STRING* string_clone(F_STRING* s, int len) F_STRING* string_clone(F_STRING* s, int len)
{ {
@ -334,18 +319,3 @@ F_STRING* string_clone(F_STRING* s, int len)
memcpy(copy + 1,s + 1,len * CHARS); memcpy(copy + 1,s + 1,len * CHARS);
return copy; return copy;
} }
void primitive_string_reverse(void)
{
F_STRING* s;
CELL capacity;
maybe_garbage_collection();
s = untag_string(dpeek());
capacity = string_capacity(s);
s = string_clone(s,capacity);
string_reverse(s,capacity);
rehash_string(s);
drepl(tag_object(s));
}

View File

@ -54,6 +54,4 @@ void primitive_string_compare(void);
void primitive_string_eq(void); void primitive_string_eq(void);
void primitive_index_of(void); void primitive_index_of(void);
void primitive_substring(void); void primitive_substring(void);
void string_reverse(F_STRING* s, int len);
F_STRING* string_clone(F_STRING* s, int len); F_STRING* string_clone(F_STRING* s, int len);
void primitive_string_reverse(void);