Expecco Code Snippets/en: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
Zeile 320: Zeile 320:
'foo/bar' asFilename binaryContents
'foo/bar' asFilename binaryContents
"/ it is good practice to ensure that the file is closed:
inStream := 'foo/bar' asFilename open.
[
...
] ensure:[
inStream close
]
"/ which is done by the convenient helper:
'foo/bar' asFilename readingFileDo:[:inStream |
[inStream atEnd] whileFalse:[
...
]
].


JavaScript
JavaScript
Zeile 341: Zeile 356:
"foo/bar".asFilename().binaryContents();
"foo/bar".asFilename().binaryContents();

// using the convenient helper:
"foo/bar".asFilename().readingFileDo(
function (inStream) {
while (! in stream.atEnd()) {
...
}
});


=== Sockets ===
=== Sockets ===

Version vom 9. Juni 2021, 12:19 Uhr

Code Snippets for Common Tasks[Bearbeiten]

Warning: this page is for geeks, programmers and coding freaks

This page is for coders of elementary actions which are to execute inside expecco (i.e. Smalltalk and JavaScript elementary actions). For bridged actions, the corresponding programming language/environment determines the syntax and set of possible operations and is outside the control of expecco.

You will find code snippets for common tasks here. Although most (or all) could also be implemented using existing actions from the standard library, it is often more convenient to write a little elementary action eg. to extract data from received binary messages or from a textual document.

You can copy-paste these snippets into the code editor and modify as appropriate. You can also paste them into a Workspace window and execute them there. Finally, you may also find those snippets via the Method Finder, when entering the desired result value.

Bits & Bytes[Bearbeiten]

Set, Clear or Toggle a Bit[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0b10011001.
"/ using masks
result := numIn bitOr: 0b00110000.    "/ to set the two bits
result := numIn bitAnd: 0b11110000.   "/ to mask bits
result := numIn bitClear: 0b00000011. "/ to clear bits
result := numIn bitXor: 0b00001100.   "/ to toggle bits
result := numIn bitTest: 0b00001100.  "/ to test if masked bits are all set

"/ using bit numbers (index starts with 1 for the least significant bit)
result := numIn bitAt: 1.             "/ to extract the first bit 
result := numIn setBit: 2.            "/ set the second bit
result := numIn clearBit: 2.          "/ clear the second bit
result := numIn invertBit: 2.         "/ toggle the second bit
 

JavaScript:

var numIn, result;

numIn = 0b10011001;
// using masks
result = numIn | 0b00110000;         // to set the two bits
result = numIn & 0b11110000;         // to mask bits
result = numIn & ˜0b00000011;        // to clear bits
result = numIn ^ 0b00001100;         // to toggle bits
result = (numIn & 0b00001100) != 0;  // to test if masked bits are all set

// using bit numbers (index starts with 1 for the least significant bit)
result = numIn.bitAt(1);             // to extract the first bit 
result = numIn.setBit(2);            // set the second bit
result = numIn.clearBit(2);          // clear the second bit
result = numIn.invertBit(2);         // toggle the second bit

Counting, Highest and Lowest Bit[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0b10011001.
result := numIn highBit.              "/ index of highest bit
result := numIn lowBit.               "/ index of lowest bit
result := numIn bitCount.             "/ count ones

JavaScript:

var numIn, result;

numIn = 0b10011001;
result = numIn.highBit();              // index of highest bit
result = numIn.lowBit();               // index of lowest bit
result = numIn.bitCount();             // count ones

Shifting and Byte Access[Bearbeiten]

Smalltalk:

|numIn result|

numIn := 0xABCD.
result := numIn bitShift: 8.       "/ left shift by 8
result := numIn bitShift: -8.      "/ right shift by 8
result := numIn << 8.              "/ alternative left shift
result := numIn >> 8.              "/ alternative right shift

result := numIn digitByteAt:1.     "/ the lowest 8 bits
result := numIn digitByteAt:2.     "/ the next 8 bits
result := numIn digitBytes.        "/ bytes as ByteArray

JavaScript:

var numIn, result;

numIn = 0xABCD;
result = numIn << 8;               // left shift by 8
result = numIn >> 8;               // right shift by 8
result = numIn.digitByteAt(1);     // as above

Accessing Bytes/Ints/Floats in a ByteArray[Bearbeiten]

Typical tasks when receiving binary encoded communication protocol messages are to extract bytes, ints, floats and strings.

Smalltalk:

|bytes result|

"/ constant byte array here; 
"/ could also come from a file, stream, message or document (see below)
bytes := #[ 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 ].

result := bytes at:1.    "/ 1 based index here
result := bytes[1].      "/ alternative syntax; also 1 based
 
result := bytes signedInt16At:2.             "/ signed int16 in machine's native byte order
result := bytes signedInt16At:3 MSB:false.   "/ in explicit lsb-order
result := bytes signedInt16At:3 MSB:true.    "/ in explicit msb-order
result := bytes signedInt32At:2.             "/ same options as above
result := bytes signedInt64At:2.             "/ same options as above
result := bytes signedInt128At:1.            "/ same options as above

result := bytes unsignedInt16At:2.           "/ same for unsigned ints
result := bytes unsignedInt16At:3 MSB:false.
result := bytes unsignedInt16At:3 MSB:true.
result := bytes unsignedInt32At:2.
result := bytes unsignedInt64At:2.

"/ writing into a byte array
bytes int16At:4 put:0x1234.                 "/ again in native byte order
bytes int16At:4 put:0x1234 MSB:true.        "/ or explicit order

"/ floats and doubles
result := bytes floatAt:3.                  "/ extract a 32bit float
result := bytes floatAt:3 MSB:true          "/ in given byte order
result := bytes doubleAt:3.                 "/ extract a 64bit float
result := bytes doubleAt:3 MSB:true         "/ in given byte order

"/ strings
result := bytes stringAt:2 size:4.               "/ extract bytes as a string
result := bytes zeroByteStringAt:3 maximumSize:3 "/ extract from a 0-terminated C-string

Byte Order and Sign Extension[Bearbeiten]

rslt := numberIn byteSwapped.     "/ swap pairwise
rslt := numberIn byteSwapped16.   "/ only the lowest 2 bytes
rslt := numberIn byteSwapped32.   "/ only the lowest 4 bytes
rslt := numberIn byteSwapped64.   "/ only the lowest 8 bytes

rslt := numberIn signExtendedByteValue.   "/ sign extent from bit 8
rslt := numberIn signExtendedShortValue.  "/ sign extent from bit 16
rslt := numberIn signExtended24BitValue.  "/ sign extent from bit 24
rslt := numberIn signExtendedLongValue.   "/ sign extent from bit 32

Numeric[Bearbeiten]

Smalltalk:

numberIn sin.                    "/ trigonometric in radians;
                                 "/ also cos, tan, sin, cosh, arcSin, arcCos etc.
numberIn degreesToRadians sin    "/ converting degrees first

n sqrt                           "/ square root
n cbrt                           "/ cubic root
 
n ln                             "/ natural logarithm
n log                            "/ decimal logarithm
n log2                           "/ binary logarithm

n raisedTo:e                     "/ raised to the power
n ** e                           "/ alternative

n1 agm: n2                       "/ geometric mean

n1 min: n2                       "/ the minimum (smaller) of the two
n1 max: n2                       "/ the maximum

JavaScript:

numberIn.sin();                  // similar
Math.sin(numberIn);              // alternative
numberIn.degreesToRadians().sin();

n.sqrt();
n.cbrt();
n.ln();  n.log();  n.log2();
n ** e                           // raised to the power
n.raisedTo(e)                    // alternative

n1.agm(n2);                      // geometric mean

Math.min(n1. n2)                 // minimum
Math.max(n1, n2)                 // maximum

SI Units and Physical Values[Bearbeiten]

Smalltalk:

1 kilo                           "/ multiplied by 1000
1 deci                           "/ divided by 10
1 mega / giga / tera             "/ various SI multipliers
1 milli / micro / nano / pico    "/

1 meter                          "/ physical length
1 kilo meter                     
1 milli meter

1 ohm / volt / ampere / newton / watt. "/ various physical units

10 milli volt / 1 kilo ohm       "/ computing with units (results is ampere)

10 degreesCelsius asFahrenheit
40 degreesFahrenheit asCelsius

100 centi meter asInches

JavaScript:

1.deci().meter()
10.milli().volt() / 1.kilo().ohm()     "/ as above

Converting Numbers to Strings and Vice Versa[Bearbeiten]

Smalltalk:

num printString                  "/ number to string (decimal)
num printStringRadix:2           "/ number to string (binary)
num printStringRadix:16          "/ number to string (hex)

num printStringRadix:16 leftPaddedTo:5 with:$0.  "/ left padded string

'%3d %02x' printf:{num1 . num2}. "/ C-printf like formatting
e'the result is: {3*num1}'       "/ string with embedded expression
 
Integer readFrom:'1234'         "/ string to number
Integer readFrom:s radix:16.    "/ hex

'%d %02x' scanf: '1234 AF'     "/ C-scanf like reading (returns an array)

String/Text Processing[Bearbeiten]

Searching[Bearbeiten]

Smalltalk:

|s|

s := 'some String with multiple Words'.
 
s includes:$S                                 "/ is character included?
s includesString:'WITH'                       "/ no - case difference
s includesString:'WITH' caseSensitive:false.  "/ yes

s indexOf:$S.                                 "/ find index of character (1 based)
s indexOfString:'Wo'.                         "/ find index of substring
s indexOfString:'WO' caseSensitive:false.     "/ case insensitive

s lastIndexOf:$e.                    "/ find last index of character
                                     "/ and similar for string and last string

s indexOf:$e startingAt:5            "/ start search at index
                                     "/ and similar for string and last string

s includesAny:'abc'                  "/ any of those characters included?
s indexOfAny:'abc'                   "/ index of any of those characters included?

s indexOfSeparator                   "/ first whitespace
s lastIndexOfSeparator               "/ last whitespace

Splitting / Truncating[Bearbeiten]

s asCollectionOfWords                "/ split into words
s splitOn:Character space            "/ ditto

s splitOn:[:ch | ch isUppercase].    "/ split with rule

s withoutSeparators                  "/ trim whitespace at both ends
s withoutLeadingSeparators           "/ at one end
s withoutTrailingSeparators

JavaScript:

var s = "some String with multiple Words";

s.splitOn($' ');
s.splitOn(function (ch) { return ch.isUppercase(); })

Searching/Extracting Parts from a Document[Bearbeiten]

|s lines|

s := someFile asFilename contentsAsString.      "/ read a file into a string
s := someFile asFilename contents.              "/ read as collection of lines

lines := s asCollectionOfLines.                 "/ convert string to a list of lines

lines findFirst:[:l | l startsWith:'foo'].      "/ search for a line; return the index
lines detect:[:l | l startsWith:'foo'].         "/ search but retrieve the line
lines count:[:l | l endsWith:'foo'].            "/ count lines for which

lines select:[:l | l includesString:'foo']      "/ extract only those lines

lines copyTo:100                                "/ the first 100 lines
lines copyLast:100                              "/ the last 100 lines
lines copyFrom:2 to:10.                         "/ some of those lines
lines copyFrom:2 butLast:10.                    "/ trim off some at either end

lines copyFrom:(lines findFirst:...)            "/ search and copy from found

lines select:[:l | l matchesRegex: '[0-9]+ [0-9]+' ]. "/ extract lines matching regex pattern
lines select:[:l | l matches: '[0-9]+*' ].            "/ extract lines matching GLOB pattern

File I/O[Bearbeiten]

Opening a file, reading, closing[Bearbeiten]

Smalltalk

|inStream line|

inStream := 'foo/bar' asFilename readStream.
[inStream atEnd] whileFalse:[
    line := inStream nextLine.
].
inStream close.

"/ get the whole contents as one big string:

'foo/bar' asFilename contentsAsString

"/ get it as a collection of lines

'foo/bar' asFilename contents

"/ get it as a big binary ByteArray 

'foo/bar' asFilename binaryContents

"/ it is good practice to ensure that the file is closed:
inStream := 'foo/bar' asFilename open.
[
   ...
] ensure:[
   inStream close
]

"/ which is done by the convenient helper:
'foo/bar' asFilename readingFileDo:[:inStream |
    [inStream atEnd] whileFalse:[
        ...
    ]
].

JavaScript

var inStream, line;

inStream = "foo/bar".asFilename().readStream();
while (! inStream.atEnd() ) {
    line = inStream.nextLine();
}
inStream.close();

// get the whole contents as one big string:

"foo/bar".asFilename().contentsAsString();

// get it as a collection of lines

"foo/bar".asFilename().contents();

// get it as a big binary ByteArray 

"foo/bar".asFilename().binaryContents();
// using the convenient helper:
"foo/bar".asFilename().readingFileDo(
    function (inStream) {
        while (! in stream.atEnd()) {
            ...
        }
    });

Sockets[Bearbeiten]

HTML[Bearbeiten]

XML[Bearbeiten]

JSON[Bearbeiten]



Copyright © 2014-2024 eXept Software AG