summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-04-13 18:24:27 +0000
committerPaul Phillips <paulp@improving.org>2010-04-13 18:24:27 +0000
commit27288e3ffe2224bb206e79204542c26a2779223b (patch)
tree399a6f78852660a4383db4f5018060eaf8c94329 /src
parent41d9ea14526b0cf64a5d2146616fd6acf3472e87 (diff)
downloadscala-27288e3ffe2224bb206e79204542c26a2779223b.tar.gz
scala-27288e3ffe2224bb206e79204542c26a2779223b.tar.bz2
scala-27288e3ffe2224bb206e79204542c26a2779223b.zip
A safety improvement for random path generation...
A safety improvement for random path generation, and a small change to Random for a more general interface. Review by community.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/io/Path.scala7
-rw-r--r--src/library/scala/util/Random.scala23
2 files changed, 19 insertions, 11 deletions
diff --git a/src/compiler/scala/tools/nsc/io/Path.scala b/src/compiler/scala/tools/nsc/io/Path.scala
index 6b88f3cfca..afef38be3c 100644
--- a/src/compiler/scala/tools/nsc/io/Path.scala
+++ b/src/compiler/scala/tools/nsc/io/Path.scala
@@ -9,9 +9,7 @@ import java.io.{
FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
BufferedInputStream, BufferedOutputStream, RandomAccessFile, File => JFile }
import java.net.{ URI, URL }
-import collection.{ Seq, Traversable }
-import PartialFunction._
-import scala.util.Random.nextASCIIString
+import scala.util.Random.alphanumeric
/** An abstraction for filesystem paths. The differences between
* Path, File, and Directory are primarily to communicate intent.
@@ -71,7 +69,8 @@ object Path
else if (jfile.isDirectory) new Directory(jfile)
else new Path(jfile)
- private[io] def randomPrefix = nextASCIIString(6)
+ /** Avoiding any shell/path issues by only using alphanumerics. */
+ private[io] def randomPrefix = alphanumeric take 6 mkString
private[io] def fail(msg: String) = throw FileOperationException(msg)
}
import Path._
diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala
index ffa248d638..cd9d5eed0e 100644
--- a/src/library/scala/util/Random.scala
+++ b/src/library/scala/util/Random.scala
@@ -89,14 +89,12 @@ class Random(val self: java.util.Random) {
List.fill(length)(safeChar()).mkString
}
- /** Returns a pseudorandomly generated String drawing upon
- * only ASCII characters between 33 and 126.
+ /** Returns the next pseudorandom, uniformly distributed value
+ * from the ASCII range 33-126.
*/
- def nextASCIIString(length: Int) = {
- val (min, max) = (33, 126)
- def nextDigit = nextInt(max - min) + min
-
- new String(Array.fill(length)(nextDigit.toByte), "ASCII")
+ def nextPrintableChar(): Char = {
+ val (low, high) = (33, 126)
+ (self.nextInt(high - low) + low).toChar
}
def setSeed(seed: Long) { self.setSeed(seed) }
@@ -111,6 +109,17 @@ object Random extends Random {
import collection.mutable.ArrayBuffer
import collection.generic.CanBuildFrom
+ /** Returns a Stream of pseudorandomly chosen alphanumeric characters,
+ * equally chosen from A-Z, a-z, and 0-9.
+ *
+ * @since 2.8
+ */
+ def alphanumeric: Stream[Char] = {
+ def isAlphaNum(c: Char) = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
+
+ Stream continually nextPrintableChar filter isAlphaNum
+ }
+
/** Returns a new collection of the same type in a randomly chosen order.
*
* @param coll the TraversableOnce to shuffle