boaold /Backend/Modules/_string.cs

Language C# Lines 264
MD5 Hash 2f53366bc52014dd691fe869fdfaf27c Estimated Cost $5,221 (why?)
Repository https://bitbucket.org/AdamMil/boaold View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
Boa is the reference implementation for a language similar to Python,
also called Boa. This implementation is both interpreted and compiled,
targeting the Microsoft .NET Framework.

http://www.adammil.net/
Copyright (C) 2004-2005 Adam Milazzo

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

using System;
using System.Collections;
using System.Text;
using Boa.Runtime;

namespace Boa.Modules
{

[BoaType("module")]
public sealed class _string
{ _string() { }

  public static string __repr__() { return "<module 'string' (built-in)>"; }
  public static string __str__() { return __repr__(); }

  public static string capitalize(string word)
  { if(word.Length==0) return word;
    StringBuilder sb = new StringBuilder(word.Length);
    sb.Append(char.ToUpper(word[0]));
    sb.Append(word.Substring(1).ToLower());
    return sb.ToString();
  }

  public static string capwords(string s)
  { List list = split(s);
    for(int i=0; i<list.Count; i++) list[i] = capitalize((string)list[i]);
    return join(list, " ");
  }

  public static string center(string s, int width)
  { width -= s.Length;
    if(width<2) return s;

    string pad = new string(' ', width/2);
    pad += s + pad;
    if((width&1)!=0) pad += ' ';
    return pad;
  }

  public static int count(string s, string sub) { return count(s, sub, 0, s.Length-1); }
  public static int count(string s, string sub, int start) { return count(s, sub, start, s.Length-1); }
  public static int count(string s, string sub, int start, int end)
  { start = Ops.FixIndex(start, s.Length);
    end   = Ops.FixIndex(end, s.Length);
    if(end<start) throw Ops.ValueError("count(): end must be >= start");
    if(start!=0 || end!=s.Length-1) s = s.Substring(start, end-start+1);

    int count=0, pos;
    while(start<=end && (pos=s.IndexOf(sub, start))!=-1) { count++; start=pos+sub.Length; }
    return count;
  }

  public static string expandtabs(string s) { return expandtabs(s, 8); }
  public static string expandtabs(string s, int tabwidth)
  { throw new NotImplementedException("this is more complicated than it seems at first");
  }

  public static string maketrans(string from, string to)
  { if(from.Length!=to.Length) throw Ops.ValueError("maketrans(): 'from' and 'to' must be the same length");
    char[] chars = new char[256];
    for(int i=0; i<256; i++) chars[i] = (char)i;
    for(int i=0; i<from.Length; i++) if(from[i]<256) chars[(int)from[i]] = to[i];
    return new string(chars);
  }

  public static int find(string s, string sub) { return find(s, sub, 0, s.Length-1); }
  public static int find(string s, string sub, int start) { return find(s, sub, start, s.Length-1); }
  public static int find(string s, string sub, int start, int end)
  { start = FixIndex(start, s.Length);
    end   = FixIndex(end, s.Length);
    return start<0 || end<0 || end<start ? -1 : s.IndexOf(sub, start, end-start+1);
  }

  public static int index(string s, string sub) { return index(s, sub, 0, s.Length-1); }
  public static int index(string s, string sub, int start) { return index(s, sub, start, s.Length-1); }
  public static int index(string s, string sub, int start, int end)
  { int pos = find(s, sub, start, end);
    if(pos==-1) throw Ops.ValueError("index(): substring not found");
    return pos;
  }

  public static string ljust(string s, int width) { return s.Length>=width ? s : s.PadRight(width); }
  public static string lower(string s) { return s.ToLower(); }

  public static string lstrip(string s) { return lstrip(s, whitespace); }
  public static string lstrip(string s, string ws) { return s.TrimStart(ws.ToCharArray()); }

  public static string join(object words) { return join(words, " "); }
  public static string join(object words, string sep)
  { IEnumerator e = Ops.GetEnumerator(words);
    StringBuilder sb = new StringBuilder();
    bool did=false;
    while(e.MoveNext())
    { if(did) sb.Append(sep);
      sb.Append(Ops.Str(e.Current));
      did = true;
    }
    return sb.ToString();
  }

  public static string replace(string s, string old, string @new) { return replace(s, old, @new, 0); }
  public static string replace(string s, string old, string @new, int maxreplace)
  { if(maxreplace<0) throw Ops.ValueError("replace(): 'maxreplace' should not be negative");
    if(maxreplace==0) return s.Replace(old, @new);
    StringBuilder sb = new StringBuilder();
    int pos=0, opos=0, reps=0;
    while(pos<s.Length && (pos=s.IndexOf(old, pos))!=-1)
    { sb.Append(s.Substring(opos, pos-opos));
      opos = pos = pos+old.Length;
      sb.Append(@new);
      if(++reps>=maxreplace) break;
    }
    if(opos<s.Length) sb.Append(s.Substring(opos));
    return sb.ToString();
  }

  public static int rfind(string s, string sub) { return rfind(s, sub, 0, s.Length-1); }
  public static int rfind(string s, string sub, int start) { return rfind(s, sub, start, s.Length-1); }
  public static int rfind(string s, string sub, int start, int end)
  { start = FixIndex(start, s.Length);
    end   = FixIndex(end, s.Length);
    return start<0 || end<0 || end<start ? -1 : s.LastIndexOf(sub, start, end-start+1);
  }

  public static int rindex(string s, string sub) { return rindex(s, sub, 0, s.Length-1); }
  public static int rindex(string s, string sub, int start) { return rindex(s, sub, start, s.Length-1); }
  public static int rindex(string s, string sub, int start, int end)
  { int pos = rfind(s, sub, start, end);
    if(pos==-1) throw Ops.ValueError("rindex(): substring not found");
    return pos;
  }

  public static string rjust(string s, int width) { return s.Length>=width ? s : s.PadLeft(width); }
  public static string rstrip(string s) { return rstrip(s, whitespace); }
  public static string rstrip(string s, string ws) { return s.TrimEnd(ws.ToCharArray()); }

  public static List split(string s) { return split(s, whitespace, 0); }
  public static List split(string s, string sep) { return split(s, sep, 0); }
  public static List split(string s, string sep, int maxsplit)
  { if(maxsplit<0) throw Ops.ValueError("split(): 'maxsplit' should not be negative");
    if(sep==null || sep=="")
    { List ret = new List(maxsplit==0 ? s.Length : maxsplit<s.Length ? maxsplit+1 : maxsplit);
      int i=0, end=Math.Min(maxsplit, s.Length);
      for(; i<end; i++) ret.append(new string(s[i], 1));
      if(i<s.Length) ret.append(s.Substring(i));
      return ret;
    }
    else
    { List ret = new List();
      int start=0, splits=0;
      for(int i=0; i<s.Length; i++)
      { char c = s[i];
        for(int j=0; j<sep.Length; j++)
          if(c==sep[j])
          { ret.append(s.Substring(start, i-start));
            start = i+1;
            if(maxsplit>0 && ++splits==maxsplit) goto done;
            break;
          }
      }
      done:
      if(start<s.Length) ret.append(start==0 ? s : s.Substring(start));
      return ret;
    }
  }

  public static string strip(string s) { return strip(s, whitespace); }
  public static string strip(string s, string ws) { return s.Trim(ws.ToCharArray()); }

  public static string swapcase(string s)
  { StringBuilder sb = new StringBuilder(s.Length);
    for(int i=0; i<s.Length; i++)
    { char u = char.ToUpper(s[i]);
      sb.Append(u==s[i] ? char.ToLower(s[i]) : u);
    }
    return sb.ToString();
  }

  public static byte[] tobytes(string s) { return Encoding.Default.GetBytes(s); }
  public static byte[] tobytes(string s, Encoding e) { return e.GetBytes(s); }

  public static string tostring(byte[] bytes) { return Encoding.Default.GetString(bytes); }
  public static string tostring(byte[] bytes, Encoding e) { return e.GetString(bytes); }

  public static string tostring(byte[] bytes, int offset, int length)
  { return Encoding.ASCII.GetString(bytes, offset, length);
  }
  public static string tostring(byte[] bytes, int offset, int length, Encoding e)
  { return e.GetString(bytes, offset, length);
  }

  public static string tostring(char[] chars) { return new string(chars); }
  public static string tostring(char[] chars, int offset, int length) { return new string(chars, offset, length); }

  public static string translate(string s, string table) { return translate(s, table, null); }
  public static string translate(string s, string table, string deletechars)
  { if(table.Length!=256) throw Ops.ValueError("translate() requires a 256-character table");
    StringBuilder sb = new StringBuilder(s.Length);
    if(deletechars==null)
      for(int i=0; i<s.Length; i++)
      { char c = s[i];
        sb.Append(table[c>255 ? 0 : (int)c]);
      }
    else
      for(int i=0; i<s.Length; i++)
      { char c = s[i];
        for(int j=0; j<deletechars.Length; i++) if(c==deletechars[j]) goto next;
        sb.Append(table[c>255 ? 0 : (int)c]);
        next:;
      }
    return sb.ToString();
  }

  public static string upper(string s) { return s.ToUpper(); }
  
  public static string zfill(string s, int width)
  { if(s.Length>=width) return s;
    string pad = new string('0', width-s.Length);
    return char.IsDigit(s[0]) ? pad+s : s[0]+pad+s.Substring(1);
  }

  public static string ascii_lowercase = "abcdefghijklmnopqrstuvwxyz";
  public static string ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  public static string ascii_letters = ascii_lowercase+ascii_uppercase;
  public static string digits = "0123456789";
  public static string hexdigits = "0123456789abcdefABCDEF";
  public static string letters = ascii_letters; // TODO: locale-dependent
  public static string lowercase = ascii_lowercase; // TODO: locale-dependent
  public static string octdigits = "01234567";
  public static string punctuation = "~`!@#$%^&*()-=_+[]{};:,<.>'\"/?\\|";
  public static string uppercase = ascii_uppercase; // TODO: locale-dependent
  public static string whitespace = " \t\n\r\f\v";
  public static string printable = digits+letters+punctuation+whitespace; // TODO: locale-dependent
  
  static int FixIndex(int index, int length)
  { if(index<0) index += length;
    else if(index>=length) index = -1;
    return index;
  }
}

} // namespace Boa.Modules
Back to Top