| 13825 | // buf[] - utf8 bytes array |
| 13826 | // max - length limit (mandatory); |
| 13827 | var utf8border = function(buf, max) { |
| 13828 | var pos; |
| 13829 | |
| 13830 | max = max || buf.length; |
| 13831 | if (max > buf.length) { max = buf.length; } |
| 13832 | |
| 13833 | // go back from last position, until start of sequence found |
| 13834 | pos = max-1; |
| 13835 | while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; } |
| 13836 | |
| 13837 | // Fuckup - very small and broken sequence, |
| 13838 | // return max, because we should return something anyway. |
| 13839 | if (pos < 0) { return max; } |
| 13840 | |
| 13841 | // If we came to start of buffer - that means vuffer is too small, |
| 13842 | // return max too. |
| 13843 | if (pos === 0) { return max; } |
| 13844 | |
| 13845 | return (pos + _utf8len[buf[pos]] > max) ? pos : max; |
| 13846 | }; |
| 13847 | |
| 13848 | // convert array to string |
| 13849 | var buf2string = function (buf) { |