forked from TheWalkingDead2/flutter_blue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in GUID library, this will most likely be replaced by publishe…
…d Uuid library
- Loading branch information
1 parent
2859b78
commit f15aa2b
Showing
4 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2017, Paul DeMarco. | ||
// All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of flutter_blue; | ||
|
||
class Guid { | ||
|
||
final List<int> _bytes; | ||
final int _hashCode; | ||
|
||
Guid._internal(List<int> bytes) : _bytes = bytes, _hashCode = _calcHashCode(bytes); | ||
|
||
Guid(String input) : this._internal(_fromString(input)); | ||
|
||
Guid.fromMac(String input) : this._internal(_fromMacString(input)); | ||
|
||
Guid.empty(): this._internal(new List.filled(16, 0)); | ||
|
||
static List<int> _fromMacString(input) { | ||
var bytes = new List<int>.filled(16, 0); | ||
|
||
if(input == null){ | ||
throw new ArgumentError("Input was null"); | ||
} | ||
input = input.toLowerCase(); | ||
|
||
final RegExp regex = new RegExp('[0-9a-f]{2}'); | ||
Iterable<Match> matches = regex.allMatches(input); | ||
|
||
if(matches.length != 6){ | ||
throw new FormatException("The format is invalid: " + input); | ||
} | ||
|
||
int i = 0; | ||
for(Match match in matches) { | ||
var hexString = input.substring(match.start,match.end); | ||
bytes[i] = hex.decode(hexString)[0]; | ||
i++; | ||
} | ||
|
||
return bytes; | ||
} | ||
|
||
static List<int> _fromString(input) { | ||
var bytes = new List<int>.filled(16, 0); | ||
if(input == null){ | ||
throw new ArgumentError("Input was null"); | ||
} | ||
if(input.length < 32){ | ||
throw new FormatException("The format is invalid"); | ||
} | ||
input = input.toLowerCase(); | ||
|
||
final RegExp regex = new RegExp('[0-9a-f]{2}'); | ||
Iterable<Match> matches = regex.allMatches(input); | ||
if(matches.length != 16){ | ||
throw new FormatException("The format is invalid"); | ||
} | ||
int i = 0; | ||
for(Match match in matches) { | ||
var hexString = input.substring(match.start,match.end); | ||
bytes[i] = hex.decode(hexString)[0]; | ||
i++; | ||
} | ||
return bytes; | ||
} | ||
|
||
static int _calcHashCode(List<int> bytes) { | ||
const equality = const ListEquality<int>(); | ||
return equality.hash(bytes); | ||
} | ||
|
||
@override | ||
String toString() { | ||
String one = hex.encode(_bytes.sublist(0,4)); | ||
String two = hex.encode(_bytes.sublist(4,6)); | ||
String three = hex.encode(_bytes.sublist(6,8)); | ||
String four = hex.encode(_bytes.sublist(8,10)); | ||
String five = hex.encode(_bytes.sublist(10,16)); | ||
return "$one-$two-$three-$four-$five"; | ||
} | ||
|
||
String toMac() { | ||
String one = hex.encode(_bytes.sublist(0,1)); | ||
String two = hex.encode(_bytes.sublist(1,2)); | ||
String three = hex.encode(_bytes.sublist(2,3)); | ||
String four = hex.encode(_bytes.sublist(3,4)); | ||
String five = hex.encode(_bytes.sublist(4,5)); | ||
String six = hex.encode(_bytes.sublist(5,6)); | ||
return "$one:$two:$three:$four:$five:$six".toUpperCase(); | ||
} | ||
|
||
List<int> toByteArray() { | ||
return _bytes; | ||
} | ||
|
||
operator ==(other) => | ||
other is Guid && this.hashCode == other.hashCode; | ||
|
||
int get hashCode => _hashCode; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2017, Paul DeMarco. | ||
// All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:flutter_blue/flutter_blue.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
main() { | ||
group("Guid", (){ | ||
test('equality', (){ | ||
var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); | ||
var guid2 = new Guid("00002a43-0000-1000-8000-00805f9b34fb"); | ||
expect(guid, guid2); | ||
|
||
var mac = new Guid.fromMac("01:02:03:04:05:06"); | ||
var mac2 = new Guid.fromMac("01:02:03:04:05:06"); | ||
expect(mac, mac2); | ||
}); | ||
|
||
test('empty()', (){ | ||
var guid = new Guid.empty(); | ||
expect("[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", guid.toByteArray().toString()); | ||
}); | ||
|
||
test('toByteArray()', (){ | ||
var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); | ||
expect("[0, 0, 42, 67, 0, 0, 16, 0, 128, 0, 0, 128, 95, 155, 52, 251]", guid.toByteArray().toString()); | ||
}); | ||
|
||
test('toString()', (){ | ||
var guid = new Guid("{00002a43-0000-1000-8000-00805f9b34fb}"); | ||
expect("00002a43-0000-1000-8000-00805f9b34fb", guid.toString()); | ||
}); | ||
|
||
test('fromMac()', (){ | ||
var guid = new Guid.fromMac("24:0A:64:50:A4:67"); | ||
expect("[36, 10, 100, 80, 164, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", guid.toByteArray().toString()); | ||
}); | ||
|
||
test('fromMac()', (){ | ||
var guid = new Guid.fromMac("24:0A:64:50:A4:67"); | ||
expect("24:0A:64:50:A4:67", guid.toMac()); | ||
}); | ||
|
||
test('hashCode', (){ | ||
var guid = new Guid.fromMac("24:0A:64:50:A4:67"); | ||
var guid2 = new Guid.fromMac("24:0A:64:50:A4:67"); | ||
expect(guid.hashCode, guid2.hashCode); | ||
}); | ||
|
||
test('empty() equality', (){ | ||
var guid = new Guid.empty(); | ||
var guid2 = new Guid.empty(); | ||
var guid3 = new Guid.fromMac("24:0A:64:50:A4:67"); | ||
expect(guid == guid2, true); | ||
expect(guid == guid3, false); | ||
}); | ||
}); | ||
|
||
} |