| 384 | } |
| 385 | |
| 386 | @StarlarkMethod( |
| 387 | name = "split", |
| 388 | doc = |
| 389 | "Returns a list of all the words in the string, using <code>sep</code> as the " |
| 390 | + "separator, optionally limiting the number of splits to <code>maxsplit</code>.", |
| 391 | parameters = { |
| 392 | @Param(name = "self", doc = "This string."), |
| 393 | @Param(name = "sep", doc = "The string to split on.", named = true), |
| 394 | @Param( |
| 395 | name = "maxsplit", |
| 396 | allowedTypes = {@ParamType(type = StarlarkInt.class)}, |
| 397 | defaultValue = "unbound", |
| 398 | doc = "The maximum number of splits.", |
| 399 | named = true) |
| 400 | }, |
| 401 | useStarlarkThread = true) |
| 402 | public StarlarkList<String> split( |
| 403 | String self, String sep, Object maxSplitO, StarlarkThread thread) throws EvalException { |
| 404 | if (sep.isEmpty()) { |
| 405 | throw Starlark.errorf("Empty separator"); |
| 406 | } |
| 407 | int maxSplit = Integer.MAX_VALUE; |
| 408 | if (maxSplitO != Starlark.UNBOUND) { |
| 409 | maxSplit = Starlark.toInt(maxSplitO, "maxsplit"); |
| 410 | } |
| 411 | StarlarkList<String> res = StarlarkList.newList(thread.mutability()); |
| 412 | int start = 0; |
| 413 | while (true) { |
| 414 | int end = self.indexOf(sep, start); |
| 415 | if (end < 0 || maxSplit-- == 0) { |
| 416 | res.addElement(self.substring(start)); |
| 417 | break; |
| 418 | } |
| 419 | res.addElement(self.substring(start, end)); |
| 420 | start = end + sep.length(); |
| 421 | } |
| 422 | return res; |
| 423 | } |
| 424 | |
| 425 | @StarlarkMethod( |
| 426 | name = "rsplit", |