10000 Add random_string.dart. · lineCode/flutter-webrtc@0a51263 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a51263

Browse files
committed
Add random_string.dart.
1 parent 6e2e6a1 commit 0a51263

File tree

3 files changed

+78
-28
lines changed

3 files changed

+78
-28
lines changed

example/lib/src/call_sample/call_sample.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'dart:io';
33
import 'dart:core';
4-
import 'dart:async';
54
import 'signaling.dart';
65
import 'package:webrtc/webrtc.dart';
76

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2016, Damon Douglas. All rights reserved. Use of this source code
2+
// is governed by a BSD-style license that can be found in the LICENSE file.
3+
4+
/// Simple library for generating random ascii strings.
5+
///
6+
/// More dartdocs go here.
7+
///
8+
///
9+
/// A simple usage example:
10+
///
11+
/// import 'package:random_string/random_string.dart' as random;
12+
/// main() {
13+
/// print(randomBetween(10,20)); // some integer between 10 and 20
14+
/// print(randomNumeric(4)); // sequence of 4 random numbers i.e. 3259
15+
/// print(randomString(10)); // random sequence of 10 characters i.e. e~f93(4l-
16+
/// print(randomAlpha(5)); // random sequence of 5 alpha characters i.e. aRztC
17+
/// print(randomAlphaNumeric(10)); // random sequence of 10 alpha numeric i.e. aRztC1y32B
18+
/// }
19+
20+
library random_string;
21+
22+
import 'dart:math';
23+
24+
const ASCII_START = 33;
25+
const ASCII_END = 126;
26+
const NUMERIC_START = 48;
27+
const NUMERIC_END = 57;
28+
const LOWER_ALPHA_START = 97;
29+
const LOWER_ALPHA_END = 122;
30+
const UPPER_ALPHA_START = 65;
31+
const UPPER_ALPHA_END = 90;
32+
33+
/// Generates a random integer where [from] <= [to].
34+
int randomBetween(int from, int to) {
35+
if (from > to) throw new Exception('$from cannot be > $to');
36+
var rand = new Random();
37+
return ((to - from) * rand.nextDouble()).toInt() + from;
38+
}
39+
40+
/// Generates a random string of [length] with characters
41+
/// between ascii [from] to [to].
42+
/// Defaults to characters of ascii '!' to '~'.
43+
String randomString(int length, {int from: ASCII_START, int to: ASCII_END}) {
44+
return new String.fromCharCodes(
45+
new List.generate(length, (index) => randomBetween(from, to)));
46+
}
47+
48+
/// Generates a random string of [length] with only numeric characters.
49+
String randomNumeric(int length) =>
50+
randomString(length, from: NUMERIC_START, to: NUMERIC_END);
51+
/*
52+
/// Generates a random string of [length] with only alpha characters.
53+
String randomAlpha(int length) {
54+
var lowerAlphaLength = randomBetween(0, length);
55+
var upperAlphaLength = length - lowerAlphaLength;
56+
var lowerAlpha = randomString(lowerAlphaLength,
57+
from: LOWER_ALPHA_START, to: LOWER_ALPHA_END);
58+
var upperAlpha = randomString(upperAlphaLength,
59+
from: UPPER_ALPHA_START, to: UPPER_ALPHA_END);
60+
return randomMerge(lowerAlpha, upperAlpha);
61+
}
62+
63+
/// Generates a random string of [length] with alpha-numeric characters.
64+
String randomAlphaNumeric(int length) {
65+
var alphaLength = randomBetween(0, length);
66+
var numericLength = length - alphaLength;
67+
var alpha = randomAlpha(alphaLength);
68+
var numeric = randomNumeric(numericLength);
69+
return randomMerge(alpha, numeric);
70+
}
71+
72+
/// Merge [a] with [b] and scramble characters.
73+
String randomMerge(String a, String b) {
74+
var mergedCodeUnits = new List.from("$a$b".codeUnits);
75+
mergedCodeUnits.shuffle();
76+
return new String.fromCharCodes(mergedCodeUnits);
77+
}*/

example/lib/src/call_sample/signaling.dart

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
import 'dart:convert';
22
import 'dart:async';
3-
import 'dart:math';
43
import 'dart:io';
54
import 'package:webrtc/webrtc.dart';
6-
7-
const ASCII_START = 33;
8-
const ASCII_END = 126;
9-
const NUMERIC_START = 48;
10-
const NUMERIC_END = 57;
11-
12-
/// Generates a random integer where [from] <= [to].
13-
int randomBetween(int from, int to) {
14-
if (from > to) throw new Exception('$from cannot be > $to');
15-
var rand = new Random();
16-
return ((to - from) * rand.nextDouble()).toInt() + from;
17-
}
18-
19-
/// Generates a random string of [length] with characters
20-
/// between ascii [from] to [to].
21-
/// Defaults to characters of ascii '!' to '~'.
22-
String randomString(int length, {int from: ASCII_START, int to: ASCII_END}) {
23-
return new String.fromCharCodes(
24-
new List.generate(length, (index) => randomBetween(from, to)));
25-
}
26-
27-
/// Generates a random string of [length] with only numeric characters.
28-
String randomNumeric(int length) =>
29-
randomString(length, from: NUMERIC_START, to: NUMERIC_END);
30-
31-
5+
import 'random_string.dart';
326

337
enum SignalingState {
348
CallStateNew,

0 commit comments

Comments
 (0)
0