Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (126 sloc) 5.34 KB
----------------------------------------------------------------------------------------------------
scx.Set: ordered maps/sets based on red-black tree
----------------------------------------------------------------------------------------------------
typedef scx.Set(T) = scx.Map(T,T)
typedef IntSet = Set(Int,Native)
//Native field should be set to 'Native' for native data types including Null<T>
//Any non-native object MUST have a well-founded ordering given by it's compare method
scx.Set(T,?Native) {
new :: ?Iterable<T> -> Set(T)
//set subtraction.
without :: Set(T) -> Set(T)
//produces a set of disjoint overlapping subsets from the two sets
//for example overlap [ {a,b,c}, {a} ]
//produces [ {a}, {b, c} ]
//
//and also [ {a,b,c,d,e}, {a,c,d}, {a,d} ]
//produces [ {a,d}, {b,e}, {c} ]
[static] overlap :: Iterable<Set(T)> -> Iterable<Set(T)>
//insert element (if doesn't exist already)
//returns true if element was inserted
insert :: T -> Bool
insert_all :: Iterable<T> -> Void
has :: T -> Bool
//set union
merge :: Set(T) -> Set(T)
intersects :: Set(T) -> Bool //non-empty intersection test
copy :: Void -> Set(T)
iterator :: Void -> Iterator<T>
empty :: Void -> Bool
size :: Void -> Int
toString :: Void -> String
//lexicographical ordering
compare :: Set(T) -> Int //{-1,0,1}
}
//U,V must be comparable/native
scx.Pair(U,NativeU,V,NativeV) {
new :: U -> V -> Pair(U,V)
//lexicographical
compare :: Pair(U,V) -> Int //{-1,0,1}
}
//Native field should be set to 'Native' for native key types including Null<T>
//Any non-native object MUST have a well-founded ordering given by it's compare method
scx.Map(Key,NativeKey,Value) {
//same methods as Set!
//except
iterator :: Void -> Iterator<RBNode(Key,Value)>
//and
has :: Key -> Bool
insert :: Key -> Value -> Bool
get :: Key -> Null<Value>
set :: Key -> Value -> Void
}
scx.RBNode(Key,Value) {
//other values
key :: Key
value :: Value
}
----------------------------------------------------------------------------------------------------
scx.Textual:
----------------------------------------------------------------------------------------------------
scx.Textual {
ARR_LIMIT : Int = 60; //max size for single array in output
//print array representation out in source code with given map
array_string : Array<T> -> (?f:T->String = Std.string) -> String
}
Example:
Textual.ARR_LIMIT = 2;
trace(Textual.array_string([1,2,3,4,5], function (x:Int) return Std.string(x*2)));
//outputs [2,4].concat([6,8]).concat([10])
----------------------------------------------------------------------------------------------------
scx.UTF: Utilities for handling Unicode strings
----------------------------------------------------------------------------------------------------
typedef UStr = Array<WChar>
typedef WChar = Int
typedef BStr = Array<Int> //byte array.
//utf mode
enum UTF {
_utf8
_utf16(big_endian:Bool)
_utf32(big_endian:Bool)
}
//Integer object for decoding
struct Integer { i:Int , new :: Int -> Void }
class scx.UStr.Unicode {
//conversions
fromString : String -> UStr
string : UStr -> String
//utils
compare : UStr -> UStr -> Int //{-1,0,1}
substr : UStr -> pos:Int -> ?cnt:Int=-1 -> UStr
wchar : String(single-char) -> WChar
utf_mode : UTF = null
//decoding a byte array to a UTF string
//if UTF=null, then will attempt to first decode a byte order mark from byte array
//if UTF!=null, then any byte order mark will be decoded anyways and may override the UTF mode
// you supply
decode :: BStr -> ?UTF=null -> UStr
//encoding a UTF string to a byte array
//if UTF=null, uses utf_mode, else sets utf_mode
//if bom is true, a byte order mark is created at start of encoded string
encode :: UStr -> ?UTF=null -> ?bom:Bool=true -> BStr
//appends byte order mark to given byte array
//if UTF=null, uses utf_mode, else sets utf_mode
//returns number of appended bytes
bom_encode :: BStr -> ?UTF=null -> Int
//decodes byte order mark from byte array
//sets utf_mode, if utf_mode is null no BOM was found
//returns utf_mode after set.
bom_decode :: BStr -> Integer -> UTF
//encodes a single UTF character with given UTF modem, appending to byte array
//if UTF=null, uses utf_mode, else sets utf_mode
//returns number of bytes written
char_encode :: BStr -> WChar -> ?UTF=null -> Int
//decodes a single UTF character with given UTF mode from byte array
//if UTF=null, uses utf_mode, else sets utf_mode
char_decode :: BStr -> Integer -> ?UTF=null -> WChar
}
----------------------------------------------------------------------------------------------------
//EXAMPLE!
creating a byte array corresponding to a utf16 little endian encoded file with BOM
using scx.UStr.Unicode;
var file:BStr = "hello there!".fromString().encode(_utf16(false));
trace(file); //[254,255,104,0,101,0,108,0,108,0,111,0,32,0,116,0,104,0,101,0,114,0,101,0,33,0]
var orig:String = file.decode().string(); //decodes BOM also.
trace(orig); //hello there!
----------------------------------------------------------------------------------------------------
Jump to Line
Something went wrong with that request. Please try again.