Ticket #281: count.patch
File count.patch, 11.0 KB (added by hopscc, 13 years ago) |
---|
-
Source/Snapshot/Cobra.Lang/Extensions.cobra
50 50 if .length == 0, return this 51 51 return this[:1].toUpper + this[1:] 52 52 53 def count (c as char) as int53 def countOf(c as char) as int 54 54 """ Return a count of the number of occurrences of the char in this string. """ 55 55 test 56 assert ''.count (c'x')==057 assert 'x'.count (c'x')==158 assert 'X'.count (c'x')==0 # case sensitive59 assert ' ! ! '.count (c'!')==256 assert ''.countOf(c'x')==0 57 assert 'x'.countOf(c'x')==1 58 assert 'X'.countOf(c'x')==0 # case sensitive 59 assert ' ! ! '.countOf(c'!')==2 60 60 body 61 61 count = 0 62 62 for ch in this -
Source/Tokenizer.cobra
586 586 if _sourceLine is nil 587 587 # end of source 588 588 return false 589 numLines = _sourceLine.count (c'\n')589 numLines = _sourceLine.countOf(c'\n') 590 590 if numLines == 0 and _willAlwaysEndWithNewLine 591 591 _sourceLine += "\n" 592 592 #trace sourceLine -
Source/Cobra.Lang/Extensions.cobra
74 74 if .length == 0, return this 75 75 return this[:1].toUpper + this[1:] 76 76 77 def count(c as char) as int 77 def count as int 78 """ 79 Return count of single chars in this string i.e this.length. 80 Analogous to count on Collections, Enumerables (list). 81 """ 82 test 83 assert ''.count == 0 84 assert 'chuck'.count == 5 85 s = 'split(sep, 2) is not easier,simpler or more obvious than break(sep)' 86 assert s.count == s.length 87 body 88 return .length 89 90 def countOf(c as char) as int 78 91 """ Return a count of the number of occurrences of the char in this string. """ 79 92 test 80 assert ''.count (c'x')==081 assert 'x'.count (c'x')==182 assert 'X'.count (c'x')==0 # case sensitive83 assert ' ! ! '.count (c'!')==293 assert ''.countOf(c'x')==0 94 assert 'x'.countOf(c'x')==1 95 assert 'X'.countOf(c'x')==0 # case sensitive 96 assert ' ! ! '.countOf(c'!')==2 84 97 body 85 98 count = 0 86 99 for ch in this, if c == ch, count += 1 87 100 return count 88 101 89 def count (substring as String) as int102 def countOf(substring as String) as int 90 103 """ Return a count of the number of occurrences of the substring in this string. """ 91 104 test 92 assert ''.count ('aoeu') == 093 assert 'x'.count ('x') == 194 assert 'xyxy'.count ('xy') == 295 assert 'xyxy'.count ('a') == 0105 assert ''.countOf('aoeu') == 0 106 assert 'x'.countOf('x') == 1 107 assert 'xyxy'.countOf('xy') == 2 108 assert 'xyxy'.countOf('a') == 0 96 109 body 97 110 return (.length - .replace(substring, '').length) // substring.length 98 111 -
Source/CommandLine.cobra
514 514 """ 515 515 516 516 get versionString as String is shared 517 ensure result.count (c'.') >= 2517 ensure result.countOf(c'.') >= 2 518 518 519 519 # Can't just take CobraCore.versionDescription as is, because that will be the one from Snapshot, 520 520 # not the current Source directory. And Snapshot can be a final release such as '0.7.4' for a … … 999 999 reMatch = Regex(r'\d+\.\d+\.\d+').match(.versionString) 1000 1000 assert reMatch.success 1001 1001 version = reMatch.value to ! 1002 assert version.count (c'.') == 2 # ex: '0.8.0'1002 assert version.countOf(c'.') == 2 # ex: '0.8.0' 1003 1003 if ' ' in .versionString or 'post' in .versionString # ex: '0.8.0 post' 1004 1004 # 'post' versions have a fourth version component of 1, as opposed to 0 1005 1005 version += '.1' 1006 assert version.count (c'.') == 31006 assert version.countOf(c'.') == 3 1007 1007 _options.addExtraUse('use System.Reflection') 1008 1008 _options.addExtraSource("assembly\n\thas AssemblyVersion('[version]')\n") 1009 1009 .doCompile(List<of String>(), true, false, nil) -
Source/Phases/BindRunTimeLibraryPhase.cobra
39 39 runTimePaths = c.commandLineArgParser.readFilesFile(filesPath) 40 40 runTimePaths.reverse 41 41 runTimeLibNativeSourceFileName = c.backEnd.runTimeLibNativeSourceFileName # 'Native.cs', 'Native.m', etc. 42 assert runTimeLibNativeSourceFileName.count (c'.') == 142 assert runTimeLibNativeSourceFileName.countOf(c'.') == 1 43 43 for runTimePath in runTimePaths 44 44 if Path.getFileName(runTimePath) == runTimeLibNativeSourceFileName 45 45 suffix = .options.embedRunTimeSuffix -
Source/BackEndClr/ScanClrType.cobra
221 221 .warning(CobraWarning('Already have declaration "[clrType.name]" in namespace "[curNameSpace.fullName]".')) 222 222 else 223 223 if type.isClass 224 if type.name.startsWith('Extend_') and type.name.count (c'_') >= 2224 if type.name.startsWith('Extend_') and type.name.countOf(c'_') >= 2 225 225 curNameSpace.addDecl(Extension(clrType, .backEnd)) 226 226 else 227 227 curNameSpace.addDecl(Class(clrType, .backEnd)) -
Source/BackEndJvm/JvmJarSig.cobra
116 116 assert fullName.contains('<') and fullName.endsWith('>') 117 117 canonName = fullName.before('<') 118 118 #params = fullName.after('<')[0:-1] # drop end '>' 119 #nParams = 1 + params.count (c',') # params.countOf(c',')119 #nParams = 1 + params.countOf(c',') # params.countOf(c',') 120 120 #assert canonName.contains('.') 121 121 #idx = canonName.lastIndexOf('.') 122 122 #gDefName = canonName[idx+1:] + '`[nParams]' … … 519 519 520 520 _isArray = true 521 521 _arrayComponentType = elementType 522 _arrayDimension = name.count (c'[')522 _arrayDimension = name.countOf(c'[') 523 523 524 524 # has properties 'length' and method 'getLength' and indexer for get and set 525 525 methodList = List<of JavaMethodInfo>() -
Source/BackEndJvm/ScanJvmType.cobra
137 137 .warning(CobraWarning('Already have declaration "[jvmNative.name]" in namespace "[curNameSpace.fullName]".')) 138 138 else 139 139 if jvmType.isClass 140 if jvmType.name.startsWith('Extend_') and jvmType.name.count (c'_') >= 2140 if jvmType.name.startsWith('Extend_') and jvmType.name.countOf(c'_') >= 2 141 141 # e.g.Extend_String - TODO 142 142 curNameSpace.addDecl(Extension(jvmNative, .backEnd)) 143 143 else -
Source/BackEndJvm/JavaGenerator.cobra
280 280 if false 281 281 # kind of silly, but it works: 282 282 d = Dictionary<of int, int>() 283 for i in 1 : File.readAllText(_javaFileName).count (c'\n')+1283 for i in 1 : File.readAllText(_javaFileName).countOf(c'\n')+1 284 284 d[i] = i 285 285 return d 286 286 return nil -
Source/CobraTokenizer.cobra
363 363 assert tok.text.endsWith(' ') 364 364 # this is okay on continued lines 365 365 if .justDidLineContinuation 366 indentLevel = tok.text.count (c'\t') + tok.text.count(c' ') // 4366 indentLevel = tok.text.countOf(c'\t') + tok.text.countOf(c' ') // 4 367 367 return _processNumIndentLevels(indentLevel) # will check continuation indentation rules 368 368 else 369 369 return .onINDENT_MIXED_TS(tok) … … 385 385 return .onINDENT_MIXED_TS(tok) 386 386 387 387 def onINDENT_ALL_TABS(tok as IToken) as IToken? 388 numTabs = tok.text.count (c'\t')388 numTabs = tok.text.countOf(c'\t') 389 389 return _processNumIndentLevels(numTabs) 390 390 391 391 def onINDENT_ALL_SPACES(tok as IToken) as IToken? 392 numSpaces = tok.text.count (c' ')392 numSpaces = tok.text.countOf(c' ') 393 393 if numSpaces % 4 and not .justDidLineContinuation # yes, 4. hard coded, intentionally. 394 394 # TODO: should really just record an error and take (numSpaces/4).round as the indent 395 395 .throwError('Space-based indentation must be a multiple of 4. This line has a remainder of [numSpaces%4].') -
Tests/200-misc/855-countOf.cobra
1 # test string extns countOf (instead of count) 2 class TestCountOf 3 def main is shared 4 # chars 5 s = 'a.b.c.d' 6 c = s.countOf(c'.') 7 assert c == 3 8 assert s.countOf(c'-') == 0 9 10 #substrings 11 assert s.countOf('.') == 3 12 assert s.countOf('-') == 0 13 assert s.countOf('.c') == 1 14 15 # chk count noargs same as length 16 assert ''.count == 0 17 assert "".count == 0 18 assert 'xyzzy'.count == 5 19 s = 'ezri dax is an excellent successor to jadzia dax' 20 assert s.count == s.length 21 -
Tests/200-misc/850-count.cobra
1 # test/ensure that String Extn count(char) is changed to be more analogous 2 # to count on other .Net things ( like Collections) - i.e no args 3 class TestCount 4 def main is shared 5 s = 'a.b.c.d' 6 c = s.count(c'.') # .error. The method "count" is expecting 0 arguments, but 1 are being supplied in this call 7 assert c == 3 # notreached -
Tests/700-command-line/900-check-complier-message-format/200-check-compiler-message-format.cobra
35 35 if 'error' in line, foundError = true # expecting an error for each file tested 36 36 assert '(' in line 37 37 assert re.match(line).success, line 38 if '(' in line, assert line.count (c'(') == 138 if '(' in line, assert line.countOf(c'(') == 1 39 39 assert foundError 40 40 41 41 def run(args as String) as String