Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

DATATYPE-NAME?

datatype-name?: func [val [string!]] [datatype? get/any load val]

CAST

cast: func [value type] [to type value]

CAN-CAST?

can-cast?: func [value type] [not error? try [to type value]]]

CAST function - Convert a value referenced by a word to a new datatype. Dialected

    cast: func [ ; convert coerce
        "Convert a value referenced by a word to a new datatype."
        input [block!] "A word, and the target datatype."
        /local words type val
    ] [
        parse input [
            some [
                copy words to 'to skip set type any-type! (
                    ;while [pos: find words 'and] [remove pos]
                    remove-each word words [word = 'and]
                    foreach word words [
                        val: get/any word
                        set/any word to either word? type [get type] [type] val
                    ]
                )
            ]
        ]
    ]
    comment {
        a: "A-0001"
        b: #B002
        c: 300
        d: <H1>
        e: 'test
        cast [a to issue!]      print mold :a
        cast [b to tag!]        print mold :b
        cast [c to decimal!]    print mold :c
        cast [a b c to string!] print remold [:a :b :c]
        cast [a b and c to issue!] print remold [:a :b :c]
        cast [
            a b and c to tag!
            and
            d and e to issue!
        ] print remold [:a :b :c :d :e]
        cast [a b c to <x>  d e to "x"] print remold [:a :b :c :d :e]
    } 

A dynamic form of the class statement.

These 2 methods are equivalent.
# method 1
class X(object):
    a = 1

# method 2
X = type('X', (object,), dict(a=1))

isinstance and issubclass

>>> class P(object):  # parent class
	pass

>>> class K(P):       # subclass
	pass

>>> k = K()           # instace
>>> isinstance(k, K)
True
>>> isinstance(k, P)
True
>>> isinstance(K, P)  # K is a class
False

>>> issubclass(K, P)
True
>>> issubclass(k, P)  # k is not a class

TypeError: issubclass() arg 1 must be a class

>>> isinstance(K, type)  # a class is an instace of type
True
>>> isinstance(k, type)  # not a class
False

get line break type (mac, linux, win and binary file)

this code analyses the first 5*1024 bytes of data from a file and tells which break type it uses, or if it's a binary file...

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

type
  TBreakType = ( btNone, btWin, btMac, btLin, btBin );

---------

function GetBreakType( const Filename: string; const MaxDataToRead: Cardinal = 5*1024 ): TBreakType;
var
  FS: TFileStream;
  Buffer, BufferStart, BufferEnd: PChar;
begin
  Result := btNone;
  if not FileExists( Filename ) then
    raise Exception.Create( 'GetBreakType: nome de arquivo inválido' );
  try
    FS := TFileStream.Create( Filename, fmOpenRead );
    GetMem( Buffer, MaxDataToRead+1 );
    BufferEnd := ( Buffer + FS.Read( Buffer^, MaxDataToRead ) );
    BufferStart := Buffer;
    BufferEnd^ := #0;
  except
    raise Exception.Create( 'GetBreakType: erro alocando memória.' );
  end;
  try
    while Buffer^ <> #0 do begin
      if Result = btNone then
        if Buffer^ = ASCII_CR then begin
          if (Buffer+1)^ = ASCII_LF then begin
            Result := btWin;
            Inc( Buffer );
          end
          else
            Result := btMac;
        end
        else if Buffer^ = ASCII_LF then
          Result := btLin;
      Inc( Buffer );
    end;
    if Buffer <> BufferEnd then
      Result := btBin;
  finally
    FreeMem( BufferStart, MaxDataToRead+1 );
    FS.Free;
  end;
end;
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS