소스 검색

Search for STL beginning to start after UTF prefix

Eran Geva 7 년 전
부모
커밋
9d76b63c27
1개의 변경된 파일21개의 추가작업 그리고 7개의 파일을 삭제
  1. 21 7
      examples/js/loaders/STLLoader.js

+ 21 - 7
examples/js/loaders/STLLoader.js

@@ -86,22 +86,36 @@ THREE.STLLoader.prototype = {
 			// However, ASCII STLs lacking the SPACE after the 'd' are known to be
 			// plentiful.  So, check the first 5 bytes for 'solid'.
 
+			// Several encodings, such as UTF-8, precede the text with up to 5 bytes:
+			// https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
+			// Search for "solid" to start anywhere after those prefixes.
+
 			// US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
 
 			var solid = [ 115, 111, 108, 105, 100 ];
 
-			for ( var i = 0; i < 5; i ++ ) {
+			for ( var off = 0; off < 5; off ++ ) {
+
+				// Check if solid[ i ] matches the i-th byte at the current offset
+
+				var found = true;
+				for ( var i = 0; found && i < 5; i ++ ) {
+
+					found = found && ( solid[ i ] == reader.getUint8( off + i, false ) );
 
-				// If solid[ i ] does not match the i-th byte, then it is not an
-				// ASCII STL; hence, it is binary and return true.
+				}
+
+				// Found "solid" text at the beginning; declare it to be an ASCII STL.
 
-				if ( solid[ i ] != reader.getUint8( i, false ) ) return true;
+				if ( found ) {
+					return false;
+				}
 
- 			}
+			}
 
-			// First 5 bytes read "solid"; declare it to be an ASCII STL
+			// Couldn't find "solid" text at the beginning; it is binary STL.
 
-			return false;
+			return true;
 
 		}