Search code examples
filefilestreamelixir

Specify alternative line_end character for Elixir File.stream! function


Elixir's File.stream! splits on and assumed \r character.

Is it possible to specify for example, \r\n or any other pattern?

Such a convenience would make File.stream! a handy file parser.

Edit: Added source file content:

 iex(1)> File.read! "D:\\Projects\\Telegram\\PQ.txt"
"1039027537039357001\r\n1124138842463513719\r\n1137145765766942221\r\n1159807134726147157\r\n1162386423249503807\r\n1166092057686212149\r\n1192934946182607263\r\n1239437837009623463\r\n1242249431735251217\r\n1286092661601003031\r\n1300223652350017207\r\n1320700236992142661\r\n1322986082402655259\r\n1342729635050601557\r\n1342815051384338027\r\n1361578683715077199\r\n1381265403472415423\r\n1387654405700676857\r\n1414719090657425471\r\n1438176310698548801\r\n1440426998028857687\r\n1444777794598883737\r\n1448786004429696643\r\n1449069084476072141\r\n1449922801627060913\r\n1459186197300152561\r\n1470497644058466497\r\n1497532721434112879\r\n1514370843858307907\r\n1528087672407582373\r\n1530255914631110911\r\n1537681216742780453\r\n1547498566041252091\r\n1563354550428106363\r\n1570520040759209689\r\n1570650619548126013\r\n1572342415580617699\r\n1595238677050713949\r\n1602246062455069687\r\n1603930707387709439\r\n1620038771342153713\r\n1626781435762382063\r\n1628817368590631491\r\n1646011824126204499\r\n1654346190847567153\r\n1671293643237388043\r\n1674249379765115707\r\n1683876665120978837\r\n1700490364729897369\r\n1724114033281923457\r\n1729626235343064671\r\n1736390408379387421\r\n1742094280210984849\r\n1750652888783086363\r\n1756848379834132853\r\n1769689620230136307\r\n1791811376213642701\r\n1802412521744570741\r\n1816018323888992941\r\n1816202297040826291\r\n1833488086890603497\r\n1834281595607491843\r\n1840295490995033057\r\n1843931859412695937\r\n1845134226412607369\r\n1847514467055999659\r\n1868936961235125427\r\n18733753

Example:

iex(134)> s|> Enum.to_list

["1039027537039357001\n", "1124138842463513719\n", "1137145765766942221\n",
 "1159807134726147157\n", "1162386423249503807\n", "1166092057686212149\n",
 "1192934946182607263\n", "1239437837009623463\n", "1242249431735251217\n",
 "1286092661601003031\n", "1300223652350017207\n", "1320700236992142661\n",
 "1322986082402655259\n", "1342729635050601557\n", "1342815051384338027\n",
 "1361578683715077199\n", "1381265403472415423\n", "1387654405700676857\n",
 "1414719090657425471\n", "1438176310698548801\n", "1440426998028857687\n",
 "1444777794598883737\n", "1448786004429696643\n", "1449069084476072141\n",
 "1449922801627060913\n", "1459186197300152561\n", "1470497644058466497\n",
 "1497532721434112879\n", "1514370843858307907\n", "1528087672407582373\n",
 "1530255914631110911\n", "1537681216742780453\n", "1547498566041252091\n",
 "1563354550428106363\n", "1570520040759209689\n", "1570650619548126013\n",
 "1572342415580617699\n", "1595238677050713949\n", "1602246062455069687\n",
 "1603930707387709439\n", "1620038771342153713\n", "1626781435762382063\n",
 "1628817368590631491\n", "1646011824126204499\n", "1654346190847567153\n",
 "1671293643237388043\n", "1674249379765115707\n", "1683876665120978837\n",
 "1700490364729897369\n", "1724114033281923457\n", ...]

iex(135)> s|> String.to_integer|> Primes.factorize|> Enum.to_list

Solution

  • Elixir handles the differences between Windows and Unix just fine by always normalizing "\r\n" into "\n", so developers don't need to worry about both formats. That's what is happening in the example above and that's what you should expect from the operations in both IO and File module.