summary refs log tree commit diff stats
path: root/lib/pure/strutils.nim
Commit message (Expand)AuthorAgeFilesLines
* Remove the uses of {.procvar.} pragma (#14359)Kaushal Modi2020-05-151-24/+24
* => supports pragmas & names (+ changed behavior) (#14200)hlaaftana2020-05-031-1/+1
* fixes #14054 [backport:1.2] (#14061)Andreas Rumpf2020-04-301-5/+2
* base `parseEnum` on a case statement, fixes #14030 (#14046)Vindaar2020-04-221-10/+83
* Tiny since cleanup (#13286)Clyybber2020-01-281-1/+1
* Rename isNilOrWhitespace to isEmptyOrWhitespace and make it use allCharsInSet...hlaaftana2020-01-261-10/+13
* replace some runtime repr in stdlib for gc:arc (#12716)Andy Davidoff2019-11-251-1/+2
* Version of trimZeros without temp strings (#12633)b3liever2019-11-111-13/+11
* remove deprecated procs (#12535)Andreas Rumpf2019-11-051-278/+0
* fix #8242, fix #12586: fix 'formatFloat' with 'precision = 0' (#12592)Miran2019-11-051-14/+6
* introduce csize_t instead of fixing csize (#12497)Arne Döring2019-10-311-2/+2
* Fix word wrappingJjp1372019-10-221-5/+6
* Fix many broken linksJjp1372019-10-221-25/+25
* Revert "Fixes #12187 (#12321)" (#12447)Andreas Rumpf2019-10-181-1/+1
* Fixes #12187 (#12321)Clyybber2019-10-081-1/+1
* Fixed sizeOf to sizeof (#12347)Ridho Pratama2019-10-031-2/+2
* [backport] run nimpretty on string stuffnarimiran2019-09-301-90/+108
* Fix spellings (#12277) [backport]Federico Ceratto2019-09-271-5/+5
* styleCheck: make the compiler and large parts of the stdlib compatible with -...Araq2019-07-101-14/+14
* [bugfix] fix `delete` in strutils and sequtils (#11535)Miran2019-06-201-1/+4
* [other] documentation: deprecation comments are now auto-generatednarimiran2019-06-191-18/+0
* Disambiguate idents now that all 3 rfind's are included in the RTL.Charles Blake2019-06-151-3/+3
* Add missing ,int to type signature in doc comment references.Charles Blake2019-06-131-3/+3
* Attempt to close https://github.com/nim-lang/Nim/issues/11430Charles Blake2019-06-121-16/+36
* fixes #11369 (#11381)Andreas Rumpf2019-06-021-10/+10
* Implement generic fromBin|Oct|Hex in strutils (#11107)Alvydas Vitkauskas2019-05-231-0/+69
* strutils.nim: fixes the indentation of formatEngAraq2019-04-101-5/+5
* Fix dead sequtils link (#10960) [ci skip]MrZoraman2019-04-041-1/+1
* strutils: document `parseEnum` [ci skip]narimiran2019-02-251-0/+23
* fixes a critical strutils bug [backport]Araq2019-02-191-1/+1
* fixes #6832Araq2019-02-191-5/+2
* documentation: remove author field [ci skip]narimiran2019-01-171-2/+0
* better docs: strutilsnarimiran2019-01-161-768/+1265
* strutils: minor code cleanupAraq2018-12-191-1/+0
* removes deprecated T/P typesAraq2018-11-161-2/+0
* better implementation for wrapWordsAraq2018-11-081-1/+2
* stripLineEnd aka chomp (#9623)Timothee Cour2018-11-051-0/+1
* Add 'dedent' to documentation index (#9615)Federico Ceratto2018-11-041-0/+1
* changelog entryArne Döring2018-10-311-1/+1
* fix typoArne Döring2018-10-311-1/+1
* fixes #9557Arne Döring2018-10-301-32/+34
* Fixes #9525 (#9526)Neelesh Chandola2018-10-281-1/+1
* Some documentation added to strutils (#9379)eqperes2018-10-161-2/+102
* math.round is not needed in strutils (#9383)skilchen2018-10-161-5/+4
* Add strip() example (#8002)Federico Ceratto2018-10-141-0/+2
* struils: don't use the deprecated accumulateResultAndreas Rumpf2018-10-141-8/+12
* added new editdistance stdlib module, deprecated strutils.editDistance, refs ...Andreas Rumpf2018-10-141-1/+4
* strutils/unicode: deprecate isLower and friends operating on complete strings...Andreas Rumpf2018-10-141-45/+12
* add strutils.stripLineEnd (#9346)Timothee Cour2018-10-131-0/+23
* Proposed solution for issue #8919 (#9280)eqperes2018-10-121-0/+2
tyle: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
#
#            Nim's Runtime Library
#        (c) Copyright 2017 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module supports helper routines for working with ``cstring``
## without having to convert ``cstring`` to ``string`` in order to
## save allocations.

include "system/inclrtl"

proc toLowerAscii(c: char): char {.inline.} =
  if c in {'A'..'Z'}:
    result = chr(ord(c) + (ord('a') - ord('A')))
  else:
    result = c

when defined(js):
  proc startsWith*(s, prefix: cstring): bool {.noSideEffect,
    importjs: "#.startsWith(#)".}

  proc endsWith*(s, suffix: cstring): bool {.noSideEffect,
    importjs: "#.endsWith(#)".}
  
  # JS string has more operations that might warrant its own module:
  # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
else:
  proc startsWith*(s, prefix: cstring): bool {.noSideEffect,
    rtl, extern: "csuStartsWith".} =
    ## Returns true if ``s`` starts with ``prefix``.
    ##
    ## If ``prefix == ""`` true is returned.
    ## 
    ## JS backend uses native ``String.prototype.startsWith``.
    var i = 0
    while true:
      if prefix[i] == '\0': return true
      if s[i] != prefix[i]: return false
      inc(i)

  proc endsWith*(s, suffix: cstring): bool {.noSideEffect,
    rtl, extern: "csuEndsWith".} =
    ## Returns true if ``s`` ends with ``suffix``.
    ##
    ## If ``suffix == ""`` true is returned.
    ## 
    ## JS backend uses native ``String.prototype.endsWith``.
    let slen = s.len
    var i = 0
    var j = slen - len(suffix)
    while i+j <% slen:
      if s[i+j] != suffix[i]: return false
      inc(i)
    if suffix[i] == '\0': return true

proc cmpIgnoreStyle*(a, b: cstring): int {.noSideEffect,
  rtl, extern: "csuCmpIgnoreStyle".} =
  ## Semantically the same as ``cmp(normalize($a), normalize($b))``. It
  ## is just optimized to not allocate temporary strings.  This should
  ## NOT be used to compare Nim identifier names. use `macros.eqIdent`
  ## for that.  Returns:
  ##
  ## | 0 if a == b
  ## | < 0 if a < b
  ## | > 0 if a > b
  ## 
  ## Not supported for JS backend, use `strutils.cmpIgnoreStyle
  ## <strutils.html#cmpIgnoreStyle%2Cstring%2Cstring>`_ instead.
  var i = 0
  var j = 0
  while true:
    while a[i] == '_': inc(i)
    while b[j] == '_': inc(j) # BUGFIX: typo
    var aa = toLowerAscii(a[i])
    var bb = toLowerAscii(b[j])
    result = ord(aa) - ord(bb)
    if result != 0 or aa == '\0': break
    inc(i)
    inc(j)

proc cmpIgnoreCase*(a, b: cstring): int {.noSideEffect,
  rtl, extern: "csuCmpIgnoreCase".} =
  ## Compares two strings in a case insensitive manner. Returns:
  ##
  ## | 0 if a == b
  ## | < 0 if a < b
  ## | > 0 if a > b
  ## 
  ## Not supported for JS backend, use `strutils.cmpIgnoreCase
  ## <strutils.html#cmpIgnoreCase%2Cstring%2Cstring>`_ instead.
  var i = 0
  while true:
    var aa = toLowerAscii(a[i])
    var bb = toLowerAscii(b[i])
    result = ord(aa) - ord(bb)
    if result != 0 or aa == '\0': break
    inc(i)