{"action":"created","issue":{"url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/362","labels_url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/362/labels{/name}","comments_url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/362/comments","events_url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/362/events","html_url":"https://github.com/JoshClose/CsvHelper/pull/362","id":62952301,"number":362,"title":"CsvWriter.cs: Fix for IndexOutOfRangeException","user":{"login":"jonathankretzmer","id":5110602,"avatar_url":"https://avatars.githubusercontent.com/u/5110602?v=3","gravatar_id":"","url":"https://api.github.com/users/jonathankretzmer","html_url":"https://github.com/jonathankretzmer","followers_url":"https://api.github.com/users/jonathankretzmer/followers","following_url":"https://api.github.com/users/jonathankretzmer/following{/other_user}","gists_url":"https://api.github.com/users/jonathankretzmer/gists{/gist_id}","starred_url":"https://api.github.com/users/jonathankretzmer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonathankretzmer/subscriptions","organizations_url":"https://api.github.com/users/jonathankretzmer/orgs","repos_url":"https://api.github.com/users/jonathankretzmer/repos","events_url":"https://api.github.com/users/jonathankretzmer/events{/privacy}","received_events_url":"https://api.github.com/users/jonathankretzmer/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-19T10:47:26Z","updated_at":"2015-03-20T20:43:16Z","closed_at":"2015-03-20T20:42:34Z","pull_request":{"url":"https://api.github.com/repos/JoshClose/CsvHelper/pulls/362","html_url":"https://github.com/JoshClose/CsvHelper/pull/362","diff_url":"https://github.com/JoshClose/CsvHelper/pull/362.diff","patch_url":"https://github.com/JoshClose/CsvHelper/pull/362.patch"},"body":"In a situation where UseExcelLeadingZerosFormatForNumerics = True, and any fields are empty, an IndexOutOfRangeException will be thrown when evaluating \" field[0] == '0' \".\r\n\r\nSwopped around the logic to test for all digits first, and then if first character is a 0.\r\n\r\nAlternatively you could introduce a test for !string.IsNullOrEmpty( field ), although I'm not sure which would be more suitable from a performance perspective?\r\n\r\nThanks.\r\n\r\nStacktrace:\r\n   at System.String.get_Chars(Int32 index)\r\n   at CsvHelper.CsvWriter.WriteField(String field, Boolean shouldQuote) in c:\\Projects\\CsvHelper\\src\\CsvHelper\\CsvWriter.cs:line 159\r\n   at CsvHelper.CsvWriter.WriteField(String field) in c:\\Projects\\CsvHelper\\src\\CsvHelper\\CsvWriter.cs:line 140\r\n   at CsvHelper.CsvWriter.WriteField(Type type, Object field) in c:\\Projects\\CsvHelper\\src\\CsvHelper\\CsvWriter.cs:line 234\r\n   at CsvHelper.CsvWriter.WriteField[T](T field) in c:\\Projects\\CsvHelper\\src\\CsvHelper\\CsvWriter.cs:line 183\r\n   at lambda_method(Closure , PatientViewModel )\r\n   at System.Action`1.Invoke(T obj)"},"comment":{"url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/comments/84139714","html_url":"https://github.com/JoshClose/CsvHelper/pull/362#issuecomment-84139714","issue_url":"https://api.github.com/repos/JoshClose/CsvHelper/issues/362","id":84139714,"user":{"login":"JoshClose","id":114971,"avatar_url":"https://avatars.githubusercontent.com/u/114971?v=3","gravatar_id":"","url":"https://api.github.com/users/JoshClose","html_url":"https://github.com/JoshClose","followers_url":"https://api.github.com/users/JoshClose/followers","following_url":"https://api.github.com/users/JoshClose/following{/other_user}","gists_url":"https://api.github.com/users/JoshClose/gists{/gist_id}","starred_url":"https://api.github.com/users/JoshClose/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoshClose/subscriptions","organizations_url":"https://api.github.com/users/JoshClose/orgs","repos_url":"https://api.github.com/users/JoshClose/repos","events_url":"https://api.github.com/users/JoshClose/events{/privacy}","received_events_url":"https://api.github.com/users/JoshClose/received_events","type":"User","site_admin":false},"created_at":"2015-03-20T20:43:16Z","updated_at":"2015-03-20T20:43:16Z","body":"I added a unit test and change the logic slightly. I'm checking the length first so it doesn't have to check every character if the field is large."}}