refactor: use extensions (#62)

This commit is contained in:
Aleksey Kulikov 2022-05-30 13:58:08 +03:00 committed by GitHub
parent 4aea9a306a
commit 3d235f5ce4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 386 additions and 355 deletions

36
lib/src/extensions.dart Normal file
View file

@ -0,0 +1,36 @@
// coverage:ignore-file
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
extension IsValidSHA on String {
bool isValidSHA() {
final hexRegExp = RegExp(r'^[0-9a-fA-F]+$');
return hexRegExp.hasMatch(this) &&
(GIT_OID_MINPREFIXLEN <= length && GIT_OID_HEXSZ >= length);
}
}
extension ToChar on String {
/// Creates a zero-terminated [Utf8] code-unit array from this String,
/// casts it to the C `char` type and returns allocated pointer to result.
Pointer<Char> toChar() => toNativeUtf8().cast<Char>();
}
extension ToDartString on Pointer<Char> {
/// Converts this UTF-8 encoded string to a Dart string.
///
/// Decodes the UTF-8 code units of this zero-terminated byte array as
/// Unicode code points and creates a Dart string containing those code
/// points.
///
/// If [length] is provided, zero-termination is ignored and the result can
/// contain NUL characters.
///
/// If [length] is not provided, the returned string is the string up til but
/// not including the first NUL character.
String toDartString({int? length}) =>
cast<Utf8>().toDartString(length: length);
}