summary refs log tree commit diff stats
path: root/lib/core/rlocks.nim
Commit message (Expand)AuthorAgeFilesLines
* move threads out of system (#20674)ringabout2022-10-291-2/+1
* sync with the same template from locks module (#18414)Antonis Geralis2021-07-101-1/+1
* alternative to #18185 (#18206)flywind2021-06-071-1/+4
* [std/rlocks]add inline pragma (#17773)flywind2021-04-191-3/+3
* replace defer with try ... finally (#17753)flywind2021-04-171-5/+5
* fix rlock compilation failure (#15584)shirleyquirk2020-10-151-1/+1
* Locks modules should give a compile error when threads are not enabled. (#12231)Ray Imber2019-09-261-0/+4
* Fix ``XDeclaredButNotUsed`` warning when locks or rlocks module is usedAnatoly Galiulin2016-03-311-0/+1
* Added rlocks module to documentation and news.txtAnatoly Galiulin2016-02-181-3/+3
* Added fixes from Araq's commentsAnatoly Galiulin2016-02-171-2/+2
* Added reentrant locks module to stdlibAnatoly Galiulin2016-02-171-0/+50
tils.nim?h=devel&id=d5ed4fba3e9225e687d916fd4129e3c61550e193'>^
2775cda6e ^


















cd9af6b80 ^


2775cda6e ^






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




















                                                                    


                                                            
 
































                                                                                           


                                                        



                                                                       



                    


                                                                                          


















                                                                


                                                                                         






                                       
#
#
#            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 iff ``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 iff ``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 iff a == b
  ## | < 0 iff a < b
  ## | > 0 iff a > b
  ## 
  ## Not supported for JS backend, use `strutils.cmpIgnoreStyle
  ## <https://nim-lang.org/docs/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 iff a == b
  ## | < 0 iff a < b
  ## | > 0 iff a > b
  ## 
  ## Not supported for JS backend, use `strutils.cmpIgnoreCase
  ## <https://nim-lang.org/docs/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)