Produce a sanitized name that fits our standards for likely to work. Java classes have a wider range of names that are technically allowed (supposedly any Unicode name) than what we support. The reason for going more narrow is to avoid situations with text encodings and converting during the pr
(String origName)
| 1780 | * because these aren't valid class names on Android. |
| 1781 | */ |
| 1782 | static public String sanitizeName(String origName) { |
| 1783 | char orig[] = origName.toCharArray(); |
| 1784 | StringBuilder sb = new StringBuilder(); |
| 1785 | |
| 1786 | // Can't lead with a digit (or anything besides a letter), so prefix with |
| 1787 | // "sketch_". In 1.x this prefixed with an underscore, but those get shaved |
| 1788 | // off later, since you can't start a sketch name with underscore anymore. |
| 1789 | if (!asciiLetter(orig[0])) { |
| 1790 | sb.append("sketch_"); |
| 1791 | } |
| 1792 | // for (int i = 0; i < orig.length; i++) { |
| 1793 | for (char c : orig) { |
| 1794 | if (asciiLetter(c) || (c >= '0' && c <= '9')) { |
| 1795 | sb.append(c); |
| 1796 | |
| 1797 | } else { |
| 1798 | // Tempting to only add if prev char is not underscore, but that |
| 1799 | // might be more confusing if lots of chars are converted and the |
| 1800 | // result is a very short string thats nothing like the original. |
| 1801 | sb.append('_'); |
| 1802 | } |
| 1803 | } |
| 1804 | // Let's not be ridiculous about the length of filenames. |
| 1805 | // in fact, Mac OS 9 can handle 255 chars, though it can't really |
| 1806 | // deal with filenames longer than 31 chars in the Finder. |
| 1807 | // Limiting to that for sketches would mean setting the |
| 1808 | // upper-bound on the character limit here to 25 characters |
| 1809 | // (to handle the base name + ".class") |
| 1810 | if (sb.length() > 63) { |
| 1811 | sb.setLength(63); |
| 1812 | } |
| 1813 | // Remove underscores from the beginning, these seem to be a reserved |
| 1814 | // thing on Android, plus it sometimes causes trouble elsewhere. |
| 1815 | int underscore = 0; |
| 1816 | while (underscore < sb.length() && sb.charAt(underscore) == '_') { |
| 1817 | underscore++; |
| 1818 | } |
| 1819 | if (underscore == sb.length()) { |
| 1820 | return "bad_sketch_name_please_fix"; |
| 1821 | |
| 1822 | } else if (underscore != 0) { |
| 1823 | return sb.substring(underscore); |
| 1824 | } |
| 1825 | return sb.toString(); |
| 1826 | } |
| 1827 | |
| 1828 | |
| 1829 | public Mode getMode() { |
no test coverage detected