Indent before equal sign in .editorconfig - ini

Is it allowed to use indent before equal sign in .editorconfig, like in php.ini?
Example:
# editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = false

Yes, it is allowed. all white spaces will be trimmed.

Related

F# scan a buffer finding the last part that begins \c\f and not followed by comma

Trying to find an elegant F# solution for this. I'm reading 1000 bytes from a file into a buffer, "buff". That part is easy.
Now, I want to scan the buffer looking for the last occurrence of a two-character combination:
Either a carriage return ('\r') or a line feed ('\f') that is not followed by a comma.
When I've found that, I need to find the next CR or LF (or the end of the buffer) and print the contents in between as a string.
Context: The file is a CSV file and I want the last line that has some non-empty value in the first column.
First of all, if you are reading CSV files, then it might be better idea to use CSV type provider. This gives you a nice typed access to CSV files and it has a couple of options that you can use for dealing with messy CSV files (e.g. if you need to skip a few lines). Alternatively, the F# Data library also has CSV parser, which lets you read the file using untyped API.
That said, if you really want to implement parsing on your own, then the following example should illustrate the idiomatic approach. I'm not sure I understand your problem exactly, but say we have:
let input = "start \r body \r, comma"
let buff = input.ToCharArray()
I believe you want to find the region between \r and \r,. You can do this using a recursive function that remembers the end of the range and the start of the range and decrements the starting range as it iterates over the string. You can use pattern matching to detect the cases that you need:
let rec findRange startLoc endLoc =
if startLoc < 0 then failwith "reached beginning"
match buff.[startLoc], buff.[startLoc+1] with
| ('\r' | '\f'), ',' -> findRange (startLoc - 1) startLoc
| ('\r' | '\f'), _ -> startLoc, endLoc
| _, _ -> findRange (startLoc - 1) endLoc
Using this, we can now get the range and get the required substring:
let s, e = findRange (buff.Length-2) (buff.Length-1)
input.Substring(s + 1, e - s - 1)
Elegant is in the eye of the beholder but one approach is implementing a matcher type. A matcher is function that given an input string and a position either succeeds returning a new matcher state with an updated position or fails.
// A matcher state holds a string and the position
[<Struct>]
type MatcherState =
{
Input : string
Pos : int
}
static member New i p : MatcherState = { Input = i ; Pos = p }
member x.Reposition p : MatcherState = { Input = x.Input ; Pos = p }
member x.AdvanceBy i : MatcherState = { Input = x.Input ; Pos = x.Pos + i }
member x.Current = x.Input.[x.Pos]
member x.InRange = x.Pos >= 0 && x.Pos < x.Input.Length
member x.Eos = x.Pos >= x.Input.Length
// A Matcher is a function that given a MatcherState
// returns Some MatcherState with a new position if successful
// otherwise returns None
type Matcher = MatcherState -> MatcherState option
By defining a few active patterns we can pattern match for the line start:
// Matches a line start
let mlineStart =
fun ms ->
match ms with
// Bad cases, new line followed by WS + Comma
| Cr (Ln (Ws (Comma _ | Eos _)))
| Ln (Ws (Comma _ | Eos _)) -> mbad
// Good cases, new line not followed by WS + Comma
| Cr (Ln (Ws ms))
| Ln (Ws ms) -> mgood ms
// All other cases bad
| _ -> mbad
Note: I handle new line followed by whitespace + comma here.
The line end is matched similar:
// Matches a line end
let mlineEnd =
fun ms ->
match ms with
// Good cases, new line or EOS
| Cr (Ln ms)
| Ln ms
| Eos ms -> mgood ms
// All other cases bad
| _ -> mbad
Finally we scanBackward looking for the line start and if we find it scanForward from that position until we find the line end.
match scanBackward testCase testCase.Length mlineStart with
| None -> printfn "No matching line start found"
| Some startPos ->
// Scan forwards from line start until we find a line end
match scanForward testCase startPos mlineEnd with
| None -> printfn "Line start found #%d, but no matching line end found" startPos
| Some endPos ->
let line = testCase.Substring (startPos, endPos - startPos)
printfn "Line found: %s" line
Matcher is actually a simplistic parser but that produces no values and that support scanning forward and backwards. The approach I have chosen is not the most efficient. If efficiency is important it can be improved by applying parser combinator techniques used by for example FParsec.
Hope this was interesting. I am sure someone can up with a shorter regex solution but what fun is that?
Full example follows (no quality guarantees given, use it as an inspiration)
// A matcher state holds a string and the position
[<Struct>]
type MatcherState =
{
Input : string
Pos : int
}
static member New i p : MatcherState = { Input = i ; Pos = p }
member x.Reposition p : MatcherState = { Input = x.Input ; Pos = p }
member x.AdvanceBy i : MatcherState = { Input = x.Input ; Pos = x.Pos + i }
member x.Current = x.Input.[x.Pos]
member x.InRange = x.Pos >= 0 && x.Pos < x.Input.Length
member x.Eos = x.Pos >= x.Input.Length
// A Matcher is a function that given a MatcherState
// returns Some MatcherState with a new position if successful
// otherwise returns None
type Matcher = MatcherState -> MatcherState option
let mgood ms = Some ms
let mbad = None
// Matches EOS
let meos : Matcher =
fun ms ->
if ms.Eos then
mgood ms
else
mbad
// Matches a specific character
let mch ch : Matcher =
fun ms ->
if not ms.InRange then
mbad
elif ms.Current = ch then
mgood <| ms.AdvanceBy 1
else mbad
// Matches zero or more whitespaces
let mws : Matcher =
fun ms ->
let rec loop pos =
if pos < ms.Input.Length then
let ch = ms.Input.[pos]
if ch = ' ' then
loop (pos + 1)
else
mgood <| ms.Reposition pos
else
mgood <| ms.Reposition pos
loop (max ms.Pos 0)
// Active patterns
let (|Eos|_|) = meos
let (|Comma|_|) = mch ','
let (|Cr|_|) = mch '\r'
let (|Ln|_|) = mch '\n'
let (|Ws|_|) = mws
// Matches a line start
let mlineStart =
fun ms ->
match ms with
// Bad cases, new line followed by WS + Comma
| Cr (Ln (Ws (Comma _ | Eos _)))
| Ln (Ws (Comma _ | Eos _)) -> mbad
// Good cases, new line not followed by WS + Comma
| Cr (Ln (Ws ms))
| Ln (Ws ms) -> mgood ms
// All other cases bad
| _ -> mbad
// Matches a line end
let mlineEnd =
fun ms ->
match ms with
// Good cases, new line or EOS
| Cr (Ln ms)
| Ln ms
| Eos ms -> mgood ms
// All other cases bad
| _ -> mbad
// Scans either backward or forward looking for a match
let scan steps input pos (m : Matcher) =
let rec loop ms =
match m ms with
| Some mms ->
if steps < 0 then
Some mms.Pos
else
Some ms.Pos
| None ->
if steps = 0 then
None
elif steps > 0 && ms.Pos >= ms.Input.Length then
None
elif steps < 0 && ms.Pos < 0 then
None
else
loop <| ms.AdvanceBy steps
loop (MatcherState.New input (min input.Length (max 0 pos)))
let scanForward = scan 1
let scanBackward = scan -1
[<EntryPoint>]
let main argv =
// Some test cases
let testCases =
[|
"""1,2,3,4
4,5,6,7"""
"""1,2,3,4
4,5,6,7
"""
"""1,2,3,4
4,5,6,7
,2,3,4
"""
"""1,2,3,4
4,5,6,7
,2,3,4
"""
|]
for testCase in testCases do
// Scan backwards from end until we find a line start
match scanBackward testCase testCase.Length mlineStart with
| None -> printfn "No matching line start found"
| Some startPos ->
// Scan forwards from line start until we find a line end
match scanForward testCase startPos mlineEnd with
| None -> printfn "Line start found #%d, but no matching line end found" startPos
| Some endPos ->
let line = testCase.Substring (startPos, endPos - startPos)
printfn "Line found: %s" line
0

Replace Number Strings in Text File

I need a code in VBScript or batch to replace 5 Caracters (the bold numbers below) in a line of a text file to change ports numbers.
change_port.vbs:
prefsFile = "%userprofile%\Desktop\teste.msrcincident"
prefsFile = CreateObject("WScript.Shell").ExpandEnvironmentStrings(prefsFile)
newPrefs = "5500"
Set fso = CreateObject("Scripting.FileSystemObject")
json = fso.OpenTextFile(prefsFile).ReadAll
Set re = New RegExp
re.Pattern = "":*?",*,"
json = re.Replace(json, ": & newPrefs & ",*,")
fso.OpenTextFile(prefsFile, 2).Write(json)
Original text file:
RCTICKET="65538,1,10.0.0.1:54593,*,ucIdnri2n4QPf/bv92mtx4w2qliCNdyDgBpHPr7nJFdxYL2/dR+iel9Mh4zgD6QR,*,*,Fbjf5rcIrdrlnibnisrzRcO8tsY=" PassStub="HG)7HbhIZPTiKy" RCTICKETENCRYPTED="1" DtStart="1457700115" DtLength="142560" L="0"/></UPLOADINFO>
Expected result text file:
RCTICKET="65538,1,10.0.0.1:5500,*,ucIdnri2n4QPf/bv92mtx4w2qliCNdyDgBpHPr7nJFdxYL2/dR+iel9Mh4zgD6QR,*,*,Fbjf5rcIrdrlnibnisrzRcO8tsY=" PassStub="HG)7HbhIZPTiKy" RCTICKETENCRYPTED="1" DtStart="1457700115" DtLength="142560" L="0"/></UPLOADINFO>
Can anyone help me?
Your search and replacement expressions are messed up. You're looking for a colon (:) followed by one or more digits (\d+ or [0-9]+) followed by a comma (,), and want to replace that with a colon followed by the new port number and a comma.
Change this:
re.Pattern = "":*?",*,"
json = re.Replace(json, ": & newPrefs & ",*,")
into this.
re.Pattern = ":\d+,"
json = re.Replace(json, ":" & newPrefs & ",")
Always keep your expressions as simple as possible.

Lua word search

How would I go about doing a multiple pattern search in Lua? (I have Lpeg set up).
For example, say I'm receiving strings in a row, I'm processing one at a time, captalizing them and calling them msg. Now I want to get msg and check if it has any of the following patterns: MUFFIN MOOPHIN MUPHEN M0FF1N for a start. How can I check if msg has any of those (doesn't matter if it's more than one) whithout having to write a huge if(or or or or)?
One thing you could do is make a table of words you want to look for, then use gmatch to iterate each word in the string and check if it's in that table.
#!/usr/bin/env lua
function matchAny(str, pats)
for w in str:gmatch('%S+') do
if pats[w] then
return true
end
end
return false
end
pats = {
['MUFFIN'] = true,
['MOOPHIN'] = true,
['MUPHEN'] = true,
['M0FF1N'] = true,
}
print(matchAny("I want a MUFFIN", pats)) -- true
print(matchAny("I want more MUFFINs", pats)) -- false
A late answer but you can construct a pattern to match all words case-insensitively (only if not followed by an alphanum), capture match position in subject and word index that is being matched with something like this:
local lpeg = require("lpeg")
local function find_words(subj, words)
local patt
for idx, word in ipairs(words) do
word = lpeg.P(word:upper()) * lpeg.Cc(idx)
patt = patt and (patt + word) or word
end
local locale = lpeg.locale()
patt = lpeg.P{ lpeg.Cp() * patt * (1 - locale.alnum) + 1 * lpeg.V(1) }
return patt:match(subj:upper())
end
local words = { "MUFFIN", "MOOPHIN", "MUPHEN", "M0FF1N" }
local pos, idx = find_words("aaaaa bbb ccc muPHEN ddd", words)
-- output: 16, 3

find and replace symbol using vbs

I am working a 1XXX words documents, I want to use vbs to changing the status of checkbox faster but I can't found any work solution, after that, i wonder if find and replace can solve my issue, so i wrote some code for this
Const wdReplaceAll = 2
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\checkbox.doc")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.Find.Text = "#1"
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE
objSelection.Find.Replacement.Text = objSelection.InsertSymbol 253, "Wingdings"
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
But, it can't work, and always show the error on the objSelection.InsertSymbol 253...
You can't use InsertSymbol in an assignment like that. Either replace the search text with an appropriate character formatted as "Wingdings":
With objSelection.Find
.Text = "#1"
.Replacement.Text = ChrW(61693)
.Replacement.Font.Name = "Wingdings"
...
End With
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
or just Find the search text and then use InsertSymbol on the selection:
objSelection.Find.Text = "#1"
objSelection.Find.Execute
objSelection.InsertSymbol 253, "Wingdings"

Python: I'm making several lists to keep track of variables in a database

It's supposed to save each line in the text file into a list and split it based on commas and categorize them into multiple different lists. The error occurs at the while loop stating that the index is out of range.
lineCounter = 0
j = 0
file = open("savefile.txt","r")
with open('savefile.txt', 'r') as f:
string = [line.strip() for line in f]
for line in file:
lineCounter += 1
while(j<lineCounter):
tempList = string[j].split(',')
firstName[j] = tempList[0]
lastName[j] = tempList[1]
postition[j] = tempList[2]
department[j] = tempList[3]
seniority[j] = tempList[4]
vacationWeeks[j] = tempList[5]
sickDays[j] = tempList[6]
iD[j] = tempList[7]
status[j] = tempList[8]
j += 1
print firstName
file.close() # close the text file
NVM the problem was that the list needed to be appended rather than replaced.

Resources