[open-ils-commits] r13006 - branches/rel_1_4_0/Open-ILS/xul/staff_client/chrome/content/util (dbs)

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Apr 27 22:28:49 EDT 2009


Author: dbs
Date: 2009-04-27 22:28:47 -0400 (Mon, 27 Apr 2009)
New Revision: 13006

Modified:
   branches/rel_1_4_0/Open-ILS/xul/staff_client/chrome/content/util/error.js
Log:
I was incrementing the counter twice in the "count linefeeds" loop. Stop doing that.
Also, use a Unicode-safe algorithm for iterating over characters.


Modified: branches/rel_1_4_0/Open-ILS/xul/staff_client/chrome/content/util/error.js
===================================================================
--- branches/rel_1_4_0/Open-ILS/xul/staff_client/chrome/content/util/error.js	2009-04-28 02:28:13 UTC (rev 13005)
+++ branches/rel_1_4_0/Open-ILS/xul/staff_client/chrome/content/util/error.js	2009-04-28 02:28:47 UTC (rev 13006)
@@ -482,13 +482,12 @@
 		var maxlines = 30;
 		var ss = '';
 		var linefeeds = 0;
-		for (var i = 0; linefeeds < maxlines && i < s.length; i++) {
-			t = s.charAt(i);
-			if (t == "\n") {
+		for (var i=0, chr; linefeeds < maxlines && i < s.length; i++) {  
+			if ((chr = this.getWholeChar(s, i)) === false) {continue;}
+			if (chr == '\u000A') { // \n
 				linefeeds++;
 			}	
-			ss = ss + t;
-			i++;
+			ss = ss + chr;
 		}
 		
 		var rv = promptService.confirmEx(window,title, ss, flags, b1, b2, b3, c, check);
@@ -527,7 +526,33 @@
 			}
 		}
 		return r;
-	}
+	},
+
+	// Copied from https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt
+	'getWholeChar' : function(str, i) {  
+		var code = str.charCodeAt(i);  
+		if (0xD800 <= code && code <= 0xDBFF) { // High surrogate(could change last hex to 0xDB7F to treat high private surrogates as single characters)  
+			if (str.length <= (i+1))  {  
+				throw 'High surrogate without following low surrogate';  
+			}  
+			var next = str.charCodeAt(i+1);  
+			if (0xDC00 > next || next > 0xDFFF) {  
+				throw 'High surrogate without following low surrogate';  
+			}  
+			return str[i]+str[i+1];  
+		}  
+		else if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate  
+			if (i === 0) {  
+				throw 'Low surrogate without preceding high surrogate';  
+			}  
+			var prev = str.charCodeAt(i-1);  
+			if (0xD800 > prev || prev > 0xDBFF) { //(could change last hex to 0xDB7F to treat high private surrogates as single characters)  
+				throw 'Low surrogate without preceding high surrogate';  
+			}  
+			return false; // We can pass over low surrogates now as the second component in a pair which we have already processed  
+		}  
+		return str[i];  
+	}  
 }
 
 dump('exiting util/error.js\n');



More information about the open-ils-commits mailing list