From 84d9081e21783151b40c0320ca50df4845043559 Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Sat, 11 Jun 2016 18:59:41 -0600 Subject: [PATCH] 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. --- doc/lib.rst | 4 +++ lib/pure/strmisc.nim | 56 ++++++++++++++++++++++++++++++ lib/pure/strutils.nim | 40 --------------------- web/news/version_0_15_released.rst | 9 +++++ web/website.ini | 2 +- 5 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 lib/pure/strmisc.nim diff --git a/doc/lib.rst b/doc/lib.rst index f749763a51..66928055a0 100644 --- a/doc/lib.rst +++ b/doc/lib.rst @@ -97,6 +97,10 @@ String handling case of a string, splitting a string into substrings, searching for substrings, replacing substrings. +* `strmisc `_ + This module contains uncommon string handling operations that do not + fit with the commonly used operations in strutils. + * `parseutils `_ This module contains helpers for parsing tokens, numbers, identifiers, etc. diff --git a/lib/pure/strmisc.nim b/lib/pure/strmisc.nim new file mode 100644 index 0000000000..359014d8ca --- /dev/null +++ b/lib/pure/strmisc.nim @@ -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 `_. + +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", "") diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index b8b3c77c7e..708f9ed4be 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -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", ""] diff --git a/web/news/version_0_15_released.rst b/web/news/version_0_15_released.rst index ad3119ccaa..2f6c2736c5 100644 --- a/web/news/version_0_15_released.rst +++ b/web/news/version_0_15_released.rst @@ -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 ------------------ diff --git a/web/website.ini b/web/website.ini index ae363afbd7..860ab9338d 100644 --- a/web/website.ini +++ b/web/website.ini @@ -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"