ASCII art diagram converter: Difference between revisions

Initial submission for Editing ASCII art diagram converter
m (→‎{{header|Wren}}: Minor tidy)
(Initial submission for Editing ASCII art diagram converter)
Line 1,669:
| |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
// RosettaCode Editing ASCII Art Diagram Converter
 
include "NSLog.incl"
 
local fn HexToBinary( hexString as CFStringRef ) as CFStringRef
NSUInteger i
CFMutableStringRef binaryString = fn MutableStringNew
CFDictionaryRef hexToBinaryMap = @{
@"0": @"0000", @"1": @"0001", @"2": @"0010", @"3": @"0011",
@"4": @"0100", @"5": @"0101", @"6": @"0110", @"7": @"0111",
@"8": @"1000", @"9": @"1001", @"A": @"1010", @"B": @"1011",
@"C": @"1100", @"D": @"1101", @"E": @"1110", @"F": @"1111"}
for i = 0 to len(hexString) - 1
CFStringRef hexDigit = ucase( fn StringSubstringWithRange( hexString, fn CFRangeMake( i, 1 ) ) )
CFStringRef binaryDigit = ucase( hexToBinaryMap[hexDigit] )
if ( binaryDigit )
MutableStringAppendString( binaryString, binaryDigit )
end if
next
end fn = binaryString
 
local fn ParseASCIIArt
CFStringRef header = @"¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
| ID |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
| QDCOUNT |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
| ANCOUNT |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
| NSCOUNT |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n¬
| ARCOUNT |\n¬
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+"
NSInteger i = 0, tempBits
CFMutableArrayRef table = fn MutableArrayNew
RegularExpressionRef regex = fn RegularExpressionWithPattern( @"\\| *\\w+ *", 0, NULL )
CFArrayRef matches = fn RegularExpressionMatches( regex, header, 0, fn CFRangeMake( 0, len(header) ) )
TextCheckingResultRef match
CFDictionaryRef dict
CFStringRef tempName, binaryStr
CFRange tempRange
for match in matches
CFRange range = fn TextCheckingResultRange( match )
CFCharacterSetRef charSet = fn CharacterSetWithCharactersInString( @"| " )
CFStringRef name = fn StringByTrimmingCharactersInSet( fn StringSubstringWithRange( header, range ), charSet )
NSInteger bits = range.length / 3
ValueRef rangeVal = fn ValueWithRange( fn CFRangeMake( i, bits ) )
CFDictionaryRef item = @{@"name":name, @"bits":fn NumberWithInteger( bits ), @"range":rangeVal}
MutableArrayAddObject( table, item )
i += bits
next
NSLog( @"\nRFC 1035 message diagram header:\n%@\n", header )
CFStringRef hexStr = @"78477bbf5496e12e1bf169a4"
CFStringRef binStr = fn HexToBinary( @"78477bbf5496e12e1bf169a4" )
NSLog( @" Decoded:" )
NSLog( @" Name Bits Start End" )
NSLog( @" ======= ==== ===== ===" )
for dict in table
tempName = dict[@"name"]
tempBits = fn NumberIntegerValue( dict[@"bits"] )
tempRange = fn ValueRange( dict[@"range"] )
NSLog( @"%8s %5ld %6ld %4ld", fn StringUTF8String( tempName ), tempBits, tempRange.location, tempRange.location + tempRange.length - 1 )
next
NSLog( @"\n Test string in hex:\n %@\n", hexStr )
NSLog( @" Test string in binary:\n %@\n", binStr )
NSLog( @" Unpacked:" )
NSLog( @" Name Size Bit Pattern" )
NSLog( @" ======= ==== ================" )
for dict in table
tempName = dict[@"name"]
tempBits = fn NumberIntegerValue( dict[@"bits"] )
tempRange = fn ValueRange( dict[@"range"] )
binaryStr = fn StringSubstringWithRange( binStr, fn CFRangeMake( tempRange.location, tempBits ) )
NSLog( @"%8s %5ld %-7s", fn StringUTF8String( tempName ), tempBits, fn StringUTF8String( binaryStr ) )
next
end fn
 
fn ParseASCIIArt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre style="height:20ex;">
 
RFC 1035 message diagram header:
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
Decoded:
Name Bits Start End
======= ==== ===== ===
ID 16 0 15
QR 1 16 16
Opcode 4 17 20
AA 1 21 21
TC 1 22 22
RD 1 23 23
RA 1 24 24
Z 3 25 27
RCODE 4 28 31
QDCOUNT 16 32 47
ANCOUNT 16 48 63
NSCOUNT 16 64 79
ARCOUNT 16 80 95
 
Test string in hex:
78477bbf5496e12e1bf169a4
 
Test string in binary:
011110000100011101111011101111110101010010010110111000010010111000011011111100010110100110100100
 
Unpacked:
Name Size Bit Pattern
======= ==== ================
ID 16 0111100001000111
QR 1 0
Opcode 4 1111
AA 1 0
TC 1 1
RD 1 1
RA 1 1
Z 3 011
RCODE 4 1111
QDCOUNT 16 0101010010010110
ANCOUNT 16 1110000100101110
NSCOUNT 16 0001101111110001
ARCOUNT 16 0110100110100100
 
</pre>
 
 
=={{header|Go}}==
729

edits