src/Pure/General/bytes.scala
changeset 80379 1f1ce661c71c
parent 80378 ab4badc7db7f
child 80380 94d903234f6b
equal deleted inserted replaced
80378:ab4badc7db7f 80379:1f1ce661c71c
   408     buf.toByteArray
   408     buf.toByteArray
   409   }
   409   }
   410 
   410 
   411   def text: String =
   411   def text: String =
   412     if (is_empty) ""
   412     if (is_empty) ""
   413     else if (byte_iterator.forall(_ >= 0)) {
   413     else {
   414       new String(make_array, UTF8.charset)
   414       var i = 0L
   415     }
   415       var utf8 = false
   416     else UTF8.decode_permissive_bytes(this)
   416       while (i < size && !utf8) {
       
   417         if (byte_unchecked(i) < 0) { utf8 = true }
       
   418         i += 1
       
   419       }
       
   420       utf8
       
   421 
       
   422       if (utf8) UTF8.decode_permissive_bytes(this)
       
   423       else new String(make_array, UTF8.charset)
       
   424     }
   417 
   425 
   418   def wellformed_text: Option[String] =
   426   def wellformed_text: Option[String] =
   419     try {
   427     try {
   420       val s = text
   428       val s = text
   421       if (this == Bytes(s)) Some(s) else None
   429       if (this == Bytes(s)) Some(s) else None