Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Oct 11, 2023
1 parent 80de836 commit d1f8b73
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Dart CI

on:
push:
branches: [ main ]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
container:
image: dart:stable
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: dart pub get
- name: Formatter
run: dart format --output none --set-exit-if-changed example lib test
- name: Analyzer
run: dart analyze --fatal-infos --fatal-warnings
- name: Tests
run: dart test --coverage=.coverage
- name: Coverage
run: dart run coverage:format_coverage -l -c -i .coverage --report-on=lib --packages=.dart_tool/package_config.json | dart run check_coverage:check_coverage
12 changes: 12 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# .github/workflows/publish.yml
name: Publish to pub.dev

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+*' # tag-pattern on pub.dev: 'v'

# Publish using the reusable workflow from dart-lang.
jobs:
publish:
uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [0.1.0] - 2023-10-10
### Added
- Initial release

[0.1.0]: https://github.com/f3ath/dart-quoted-string/releases/tag/0.1.0
6 changes: 6 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include: package:lints/recommended.yaml

linter:
rules:
- sort_constructors_first
- sort_unnamed_constructors_first
9 changes: 9 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:quoted_string/quoted_string.dart';

void main() {
// "Quote (\") and backslash (\\)"
print(r'Quote (") and backslash (\)'.quote());

// Quote (") and backslash (\)
print(r'"Quote (\") and backslash (\\)"'.unquote());
}
19 changes: 19 additions & 0 deletions lib/quoted_string.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension QuotedString on String {
/// Encloses the string into double quotes. Escapes the content if necessary.
String quote() => '"${replaceAll(r'\', r'\\').replaceAll('"', r'\"')}"';

/// Removes the quotes around the string and unescapes it.
/// If this string is not a quoted string, returns it unchanged.
String unquote() {
if (length < 2 || !startsWith('"') || !endsWith('"')) return this;
final buf = StringBuffer();
for (var i = 1; i < length - 1; i++) {
if (this[i] == r'\') {
if (i > length - 3) return this;
i++;
}
buf.write(this[i]);
}
return buf.toString();
}
}
18 changes: 18 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: quoted_string
description: String quoting/unquoting.
version: 0.1.0
repository: https://github.com/f3ath/dart-quoted-string

environment:
sdk: '>=3.0.0 <4.0.0'

dev_dependencies:
lints: ^2.0.1
cider: ^0.2.3
test: ^1.21.1
check_coverage: ^0.0.2

cider:
link_template:
tag: 'https://github.com/f3ath/dart-quoted-string/releases/tag/%tag%'
diff: 'https://github.com/f3ath/dart-quoted-string/compare/%from%...%to%'
36 changes: 36 additions & 0 deletions test/functional_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:quoted_string/quoted_string.dart';
import 'package:test/test.dart';

void main() {
group('Quoting', () {
const cases = {
r'"hello"\world ': r'"\"hello\"\\world "',
r'': r'""',
r'"': r'"\""',
r'\': r'"\\"',
};
cases.forEach((unquoted, quoted) {
test('<$unquoted> => <$quoted>', () {
expect(unquoted.quote(), equals(quoted));
});
});
});

group('Unquoting', () {
const cases = {
'': '',
'foo': 'foo',
'"': '"',
'""': '',
r'"\\"': r'\',
r'"\"': r'"\"',
r'"\\\"': r'"\\\"',
r'"\\\""': r'\"',
};
cases.forEach((quoted, unquoted) {
test('<$quoted> => <$unquoted>', () {
expect(quoted.unquote(), equals(unquoted));
});
});
});
}

0 comments on commit d1f8b73

Please sign in to comment.