mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-05 03:14:08 +00:00
Move partition and rpartition to new strmisc module
This was done because partition is an uncommonly used proc that is still useful in rare cases. There is also a desire to add more procs to this module at a later time.
This commit is contained in:
@@ -97,6 +97,10 @@ String handling
|
||||
case of a string, splitting a string into substrings, searching for
|
||||
substrings, replacing substrings.
|
||||
|
||||
* `strmisc <strmisc.html>`_
|
||||
This module contains uncommon string handling operations that do not
|
||||
fit with the commonly used operations in strutils.
|
||||
|
||||
* `parseutils <parseutils.html>`_
|
||||
This module contains helpers for parsing tokens, numbers, identifiers, etc.
|
||||
|
||||
|
||||
56
lib/pure/strmisc.nim
Normal file
56
lib/pure/strmisc.nim
Normal file
@@ -0,0 +1,56 @@
|
||||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2016 Joey Payne
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module contains various string utility routines that are uncommonly
|
||||
## used in comparison to `strutils <strutils.html>`_.
|
||||
|
||||
import strutils
|
||||
|
||||
{.deadCodeElim: on.}
|
||||
|
||||
proc partition*(s: string, sep: string,
|
||||
right: bool = false): (string, string, string)
|
||||
{.noSideEffect, procvar.} =
|
||||
## Split the string at the first or last occurrence of `sep` into a 3-tuple
|
||||
##
|
||||
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
|
||||
## (`s`, "", "") if `sep` is not found and `right` is false or
|
||||
## ("", "", `s`) if `sep` is not found and `right` is true
|
||||
|
||||
let position = if right: s.rfind(sep) else: s.find(sep)
|
||||
|
||||
if position != -1:
|
||||
let
|
||||
beforeSep = s[0 ..< position]
|
||||
afterSep = s[position + sep.len ..< s.len]
|
||||
|
||||
return (beforeSep, sep, afterSep)
|
||||
|
||||
return if right: ("", "", s) else: (s, "", "")
|
||||
|
||||
proc rpartition*(s: string, sep: string): (string, string, string)
|
||||
{.noSideEffect, procvar.} =
|
||||
## Split the string at the last occurrence of `sep` into a 3-tuple
|
||||
##
|
||||
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
|
||||
## ("", "", `s`) if `sep` is not found
|
||||
return partition(s, sep, right = true)
|
||||
|
||||
when isMainModule:
|
||||
doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
|
||||
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
|
||||
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
|
||||
doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
|
||||
doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
|
||||
|
||||
doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
|
||||
doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
|
||||
doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
|
||||
doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
|
||||
doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")
|
||||
@@ -1210,34 +1210,6 @@ proc rfind*(s: string, sub: char, start: int = -1): int {.noSideEffect,
|
||||
if sub == s[i]: return i
|
||||
return -1
|
||||
|
||||
proc partition*(s: string, sep: string,
|
||||
right: bool = false): (string, string, string)
|
||||
{.noSideEffect, procvar, rtl, extern: "nsuPartitionStr".} =
|
||||
## Split the string at the first or last occurrence of `sep` into a 3-tuple
|
||||
##
|
||||
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
|
||||
## (`s`, "", "") if `sep` is not found and `right` is false or
|
||||
## ("", "", `s`) if `sep` is not found and `right` is true
|
||||
|
||||
let position = if right: s.rfind(sep) else: s.find(sep)
|
||||
|
||||
if position != -1:
|
||||
let
|
||||
beforeSep = s[0 ..< position]
|
||||
afterSep = s[position + sep.len ..< s.len]
|
||||
|
||||
return (s[0 ..< position], sep, afterSep)
|
||||
|
||||
return if right: ("", "", s) else: (s, "", "")
|
||||
|
||||
proc rpartition*(s: string, sep: string): (string, string, string)
|
||||
{.noSideEffect, procvar, rtl, extern: "nsuRPartitionStr".} =
|
||||
## Split the string at the last occurrence of `sep` into a 3-tuple
|
||||
##
|
||||
## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
|
||||
## ("", "", `s`) if `sep` is not found
|
||||
return partition(s, sep, right = true)
|
||||
|
||||
proc center*(s: string, width: int, fillChar: char = ' '): string {.
|
||||
noSideEffect, rtl, extern: "nsuCenterString".} =
|
||||
## Return the contents of `s` centered in a string `width` long using
|
||||
@@ -2243,18 +2215,6 @@ when isMainModule:
|
||||
doAssert expandTabs("", 0) == ""
|
||||
doAssert expandTabs("\t\t\t", 0) == ""
|
||||
|
||||
doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
|
||||
doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
|
||||
doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
|
||||
doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
|
||||
doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
|
||||
|
||||
doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
|
||||
doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
|
||||
doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
|
||||
doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
|
||||
doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")
|
||||
|
||||
doAssert rsplit("foo bar", seps=Whitespace) == @["foo", "bar"]
|
||||
doAssert rsplit(" foo bar", seps=Whitespace, maxsplit=1) == @[" foo", "bar"]
|
||||
doAssert rsplit(" foo bar ", seps=Whitespace, maxsplit=1) == @[" foo bar", ""]
|
||||
|
||||
@@ -17,6 +17,15 @@ Library Additions
|
||||
- Added ``readHeaderRow`` and ``rowEntry`` to ``parsecsv.nim`` to provide
|
||||
a lightweight alternative to python's ``csv.DictReader``.
|
||||
|
||||
- Added ``center``, ``rsplit``, and ``expandTabs`` to ``strutils.nim`` to
|
||||
provide similar Python functionality for Nim's strings.
|
||||
|
||||
- Added ``isTitle``, ``title``, and ``swapCase`` to ``unicode.nim`` to
|
||||
provide unicode aware string case manipulation.
|
||||
|
||||
- Added a new module ``lib/pure/strmisc.nim`` to hold uncommon string
|
||||
operations. Currently contains ``partition`` and ``rpartition``.
|
||||
|
||||
Compiler Additions
|
||||
------------------
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ srcdoc2: "pure/stats;impure/nre;windows/winlean;pure/random"
|
||||
srcdoc2: "pure/complex;pure/times;pure/osproc;pure/pegs;pure/dynlib;pure/strscans"
|
||||
srcdoc2: "pure/parseopt;pure/parseopt2;pure/hashes;pure/strtabs;pure/lexbase"
|
||||
srcdoc2: "pure/parsecfg;pure/parsexml;pure/parsecsv;pure/parsesql"
|
||||
srcdoc2: "pure/streams;pure/terminal;pure/cgi;pure/unicode"
|
||||
srcdoc2: "pure/streams;pure/terminal;pure/cgi;pure/unicode;pure/strmisc"
|
||||
srcdoc2: "pure/htmlgen;pure/parseutils;pure/browsers"
|
||||
srcdoc2: "impure/db_postgres;impure/db_mysql;impure/db_sqlite"
|
||||
srcdoc2: "pure/httpserver;pure/httpclient;pure/smtp;impure/ssl;pure/fsmonitor"
|
||||
|
||||
Reference in New Issue
Block a user