PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Backend/Modules/_string.cs

https://bitbucket.org/AdamMil/boaold
C# | 263 lines | 209 code | 34 blank | 20 comment | 46 complexity | 2f53366bc52014dd691fe869fdfaf27c MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.Collections;
  21. using System.Text;
  22. using Boa.Runtime;
  23. namespace Boa.Modules
  24. {
  25. [BoaType("module")]
  26. public sealed class _string
  27. { _string() { }
  28. public static string __repr__() { return "<module 'string' (built-in)>"; }
  29. public static string __str__() { return __repr__(); }
  30. public static string capitalize(string word)
  31. { if(word.Length==0) return word;
  32. StringBuilder sb = new StringBuilder(word.Length);
  33. sb.Append(char.ToUpper(word[0]));
  34. sb.Append(word.Substring(1).ToLower());
  35. return sb.ToString();
  36. }
  37. public static string capwords(string s)
  38. { List list = split(s);
  39. for(int i=0; i<list.Count; i++) list[i] = capitalize((string)list[i]);
  40. return join(list, " ");
  41. }
  42. public static string center(string s, int width)
  43. { width -= s.Length;
  44. if(width<2) return s;
  45. string pad = new string(' ', width/2);
  46. pad += s + pad;
  47. if((width&1)!=0) pad += ' ';
  48. return pad;
  49. }
  50. public static int count(string s, string sub) { return count(s, sub, 0, s.Length-1); }
  51. public static int count(string s, string sub, int start) { return count(s, sub, start, s.Length-1); }
  52. public static int count(string s, string sub, int start, int end)
  53. { start = Ops.FixIndex(start, s.Length);
  54. end = Ops.FixIndex(end, s.Length);
  55. if(end<start) throw Ops.ValueError("count(): end must be >= start");
  56. if(start!=0 || end!=s.Length-1) s = s.Substring(start, end-start+1);
  57. int count=0, pos;
  58. while(start<=end && (pos=s.IndexOf(sub, start))!=-1) { count++; start=pos+sub.Length; }
  59. return count;
  60. }
  61. public static string expandtabs(string s) { return expandtabs(s, 8); }
  62. public static string expandtabs(string s, int tabwidth)
  63. { throw new NotImplementedException("this is more complicated than it seems at first");
  64. }
  65. public static string maketrans(string from, string to)
  66. { if(from.Length!=to.Length) throw Ops.ValueError("maketrans(): 'from' and 'to' must be the same length");
  67. char[] chars = new char[256];
  68. for(int i=0; i<256; i++) chars[i] = (char)i;
  69. for(int i=0; i<from.Length; i++) if(from[i]<256) chars[(int)from[i]] = to[i];
  70. return new string(chars);
  71. }
  72. public static int find(string s, string sub) { return find(s, sub, 0, s.Length-1); }
  73. public static int find(string s, string sub, int start) { return find(s, sub, start, s.Length-1); }
  74. public static int find(string s, string sub, int start, int end)
  75. { start = FixIndex(start, s.Length);
  76. end = FixIndex(end, s.Length);
  77. return start<0 || end<0 || end<start ? -1 : s.IndexOf(sub, start, end-start+1);
  78. }
  79. public static int index(string s, string sub) { return index(s, sub, 0, s.Length-1); }
  80. public static int index(string s, string sub, int start) { return index(s, sub, start, s.Length-1); }
  81. public static int index(string s, string sub, int start, int end)
  82. { int pos = find(s, sub, start, end);
  83. if(pos==-1) throw Ops.ValueError("index(): substring not found");
  84. return pos;
  85. }
  86. public static string ljust(string s, int width) { return s.Length>=width ? s : s.PadRight(width); }
  87. public static string lower(string s) { return s.ToLower(); }
  88. public static string lstrip(string s) { return lstrip(s, whitespace); }
  89. public static string lstrip(string s, string ws) { return s.TrimStart(ws.ToCharArray()); }
  90. public static string join(object words) { return join(words, " "); }
  91. public static string join(object words, string sep)
  92. { IEnumerator e = Ops.GetEnumerator(words);
  93. StringBuilder sb = new StringBuilder();
  94. bool did=false;
  95. while(e.MoveNext())
  96. { if(did) sb.Append(sep);
  97. sb.Append(Ops.Str(e.Current));
  98. did = true;
  99. }
  100. return sb.ToString();
  101. }
  102. public static string replace(string s, string old, string @new) { return replace(s, old, @new, 0); }
  103. public static string replace(string s, string old, string @new, int maxreplace)
  104. { if(maxreplace<0) throw Ops.ValueError("replace(): 'maxreplace' should not be negative");
  105. if(maxreplace==0) return s.Replace(old, @new);
  106. StringBuilder sb = new StringBuilder();
  107. int pos=0, opos=0, reps=0;
  108. while(pos<s.Length && (pos=s.IndexOf(old, pos))!=-1)
  109. { sb.Append(s.Substring(opos, pos-opos));
  110. opos = pos = pos+old.Length;
  111. sb.Append(@new);
  112. if(++reps>=maxreplace) break;
  113. }
  114. if(opos<s.Length) sb.Append(s.Substring(opos));
  115. return sb.ToString();
  116. }
  117. public static int rfind(string s, string sub) { return rfind(s, sub, 0, s.Length-1); }
  118. public static int rfind(string s, string sub, int start) { return rfind(s, sub, start, s.Length-1); }
  119. public static int rfind(string s, string sub, int start, int end)
  120. { start = FixIndex(start, s.Length);
  121. end = FixIndex(end, s.Length);
  122. return start<0 || end<0 || end<start ? -1 : s.LastIndexOf(sub, start, end-start+1);
  123. }
  124. public static int rindex(string s, string sub) { return rindex(s, sub, 0, s.Length-1); }
  125. public static int rindex(string s, string sub, int start) { return rindex(s, sub, start, s.Length-1); }
  126. public static int rindex(string s, string sub, int start, int end)
  127. { int pos = rfind(s, sub, start, end);
  128. if(pos==-1) throw Ops.ValueError("rindex(): substring not found");
  129. return pos;
  130. }
  131. public static string rjust(string s, int width) { return s.Length>=width ? s : s.PadLeft(width); }
  132. public static string rstrip(string s) { return rstrip(s, whitespace); }
  133. public static string rstrip(string s, string ws) { return s.TrimEnd(ws.ToCharArray()); }
  134. public static List split(string s) { return split(s, whitespace, 0); }
  135. public static List split(string s, string sep) { return split(s, sep, 0); }
  136. public static List split(string s, string sep, int maxsplit)
  137. { if(maxsplit<0) throw Ops.ValueError("split(): 'maxsplit' should not be negative");
  138. if(sep==null || sep=="")
  139. { List ret = new List(maxsplit==0 ? s.Length : maxsplit<s.Length ? maxsplit+1 : maxsplit);
  140. int i=0, end=Math.Min(maxsplit, s.Length);
  141. for(; i<end; i++) ret.append(new string(s[i], 1));
  142. if(i<s.Length) ret.append(s.Substring(i));
  143. return ret;
  144. }
  145. else
  146. { List ret = new List();
  147. int start=0, splits=0;
  148. for(int i=0; i<s.Length; i++)
  149. { char c = s[i];
  150. for(int j=0; j<sep.Length; j++)
  151. if(c==sep[j])
  152. { ret.append(s.Substring(start, i-start));
  153. start = i+1;
  154. if(maxsplit>0 && ++splits==maxsplit) goto done;
  155. break;
  156. }
  157. }
  158. done:
  159. if(start<s.Length) ret.append(start==0 ? s : s.Substring(start));
  160. return ret;
  161. }
  162. }
  163. public static string strip(string s) { return strip(s, whitespace); }
  164. public static string strip(string s, string ws) { return s.Trim(ws.ToCharArray()); }
  165. public static string swapcase(string s)
  166. { StringBuilder sb = new StringBuilder(s.Length);
  167. for(int i=0; i<s.Length; i++)
  168. { char u = char.ToUpper(s[i]);
  169. sb.Append(u==s[i] ? char.ToLower(s[i]) : u);
  170. }
  171. return sb.ToString();
  172. }
  173. public static byte[] tobytes(string s) { return Encoding.Default.GetBytes(s); }
  174. public static byte[] tobytes(string s, Encoding e) { return e.GetBytes(s); }
  175. public static string tostring(byte[] bytes) { return Encoding.Default.GetString(bytes); }
  176. public static string tostring(byte[] bytes, Encoding e) { return e.GetString(bytes); }
  177. public static string tostring(byte[] bytes, int offset, int length)
  178. { return Encoding.ASCII.GetString(bytes, offset, length);
  179. }
  180. public static string tostring(byte[] bytes, int offset, int length, Encoding e)
  181. { return e.GetString(bytes, offset, length);
  182. }
  183. public static string tostring(char[] chars) { return new string(chars); }
  184. public static string tostring(char[] chars, int offset, int length) { return new string(chars, offset, length); }
  185. public static string translate(string s, string table) { return translate(s, table, null); }
  186. public static string translate(string s, string table, string deletechars)
  187. { if(table.Length!=256) throw Ops.ValueError("translate() requires a 256-character table");
  188. StringBuilder sb = new StringBuilder(s.Length);
  189. if(deletechars==null)
  190. for(int i=0; i<s.Length; i++)
  191. { char c = s[i];
  192. sb.Append(table[c>255 ? 0 : (int)c]);
  193. }
  194. else
  195. for(int i=0; i<s.Length; i++)
  196. { char c = s[i];
  197. for(int j=0; j<deletechars.Length; i++) if(c==deletechars[j]) goto next;
  198. sb.Append(table[c>255 ? 0 : (int)c]);
  199. next:;
  200. }
  201. return sb.ToString();
  202. }
  203. public static string upper(string s) { return s.ToUpper(); }
  204. public static string zfill(string s, int width)
  205. { if(s.Length>=width) return s;
  206. string pad = new string('0', width-s.Length);
  207. return char.IsDigit(s[0]) ? pad+s : s[0]+pad+s.Substring(1);
  208. }
  209. public static string ascii_lowercase = "abcdefghijklmnopqrstuvwxyz";
  210. public static string ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  211. public static string ascii_letters = ascii_lowercase+ascii_uppercase;
  212. public static string digits = "0123456789";
  213. public static string hexdigits = "0123456789abcdefABCDEF";
  214. public static string letters = ascii_letters; // TODO: locale-dependent
  215. public static string lowercase = ascii_lowercase; // TODO: locale-dependent
  216. public static string octdigits = "01234567";
  217. public static string punctuation = "~`!@#$%^&*()-=_+[]{};:,<.>'\"/?\\|";
  218. public static string uppercase = ascii_uppercase; // TODO: locale-dependent
  219. public static string whitespace = " \t\n\r\f\v";
  220. public static string printable = digits+letters+punctuation+whitespace; // TODO: locale-dependent
  221. static int FixIndex(int index, int length)
  222. { if(index<0) index += length;
  223. else if(index>=length) index = -1;
  224. return index;
  225. }
  226. }
  227. } // namespace Boa.Modules