update @ Thu 20 Jan 16:59:33 EET 2022

Signed-off-by: Ari Archer <truncateddinosour@gmail.com>
This commit is contained in:
Ari Archer 2022-01-20 16:59:33 +02:00
parent d8604ac3d5
commit e07bc9774b
4 changed files with 129 additions and 5 deletions

View file

@ -0,0 +1,116 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>
Ari::web -&gt; Blog
</title>
<meta content="website webdev linux programming ari terminal dark blog javascript opensource free" name="keywords"/>
<meta content="Blog on 2022-01-20 16:58:31 EET - Fast atoi in C(++)" name="description"/>
<meta content="follow" name="robots"/>
</head>
<body>
<div>
<h1>
Fast atoi in C(++)
</h1>
<p>
2022-01-20 16:58:31 EET |
<a href="..">
back
</a>
|
<a href="/">
home
</a>
|
<a href="/git">
git
</a>
</p>
<hr/>
<p>
<code>
std::atoi
</code>
(in C++) is fast on its own but can be slow
so for stuff you need speed you can use this:
</p>
<ul>
<li>
Option one
</li>
</ul>
<p>
```
int fast_atoi(const char *int_string) {
int value = 0;
</p>
<pre><code>while (*int_string &amp;&amp; *int_string &gt;= '0' &amp;&amp; *int_string &lt;= '9')
value = value * 10 + (*int_string++ - '0');
return value;
</code></pre>
<p>
}
```
</p>
<p>
This requires no headers, just pure C (also valid in C++),
you can put this into your code and it will just work!
</p>
<ul>
<li>
Option two
</li>
</ul>
<p>
```
</p>
<h1>
include
<assert.h>
</assert.h>
</h1>
<p>
int fast_atoi(char *int_string) {
int value = 0;
</p>
<pre><code>while (*int_string) {
if (!(*int_string &gt;= '0' &amp;&amp; *int_string &lt;= '9'))
assert(0 &amp;&amp; "atoi(): invalid int_string");
value = value * 10 + (*int_string++ - '0');
}
return value;
</code></pre>
<p>
}
```
</p>
<p>
This option is throws an assertion error if it encounters an
invalid string but this requires
<code>
assert.h
</code>
as a dependency.
</p>
<p>
In C++
<code>
assert.h
</code>
should be replaced with
<code>
cassert
</code>
.
</p>
</div>
</body>
</html>
<!-- this is automatically generated by scripts/add_blog -->

View file

@ -16,7 +16,7 @@
My blogs My blogs
</h1> </h1>
<code> <code>
Last updated on: 2022-01-20 16:11:48 EET Last updated on: 2022-01-20 16:58:31 EET
</code> </code>
<div> <div>
<ul> <ul>
@ -135,6 +135,11 @@
Fixing kernel freezing on realtek WiFi Fixing kernel freezing on realtek WiFi
</a> </a>
</li> </li>
<li>
<a href="/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html">
Fast atoi in C(++)
</a>
</li>
</ul> </ul>
</div> </div>
</body> </body>

View file

@ -21,3 +21,4 @@
* [New domain! -- ari-web.xyz](/page/blog/blogs/New-domain!----ari-web.xyz_7375640347.html) * [New domain! -- ari-web.xyz](/page/blog/blogs/New-domain!----ari-web.xyz_7375640347.html)
* [Funny thing about my site](/page/blog/blogs/Funny-thing-about-my-site_6342443591.html) * [Funny thing about my site](/page/blog/blogs/Funny-thing-about-my-site_6342443591.html)
* [Fixing kernel freezing on realtek WiFi](/page/blog/blogs/Fixing-kernel-freezing-on-realtek-wifi_5342268108.html) * [Fixing kernel freezing on realtek WiFi](/page/blog/blogs/Fixing-kernel-freezing-on-realtek-wifi_5342268108.html)
* [Fast atoi in C(++)](/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html)

View file

@ -2,11 +2,11 @@
""" Adds a new blog """ """ Adds a new blog """
import os import os
import random
import subprocess import subprocess
import sys import sys
import markdown
import random
import markdown
from bs4 import BeautifulSoup as bs from bs4 import BeautifulSoup as bs
CHARACTER_MAP = {"/": "_", " ": "-", "?": "_", "#": "_", "<": "_", ">": "_", "\\": "_"} CHARACTER_MAP = {"/": "_", " ": "-", "?": "_", "#": "_", "<": "_", ">": "_", "\\": "_"}
@ -71,7 +71,9 @@ def sanitise_title(title: str) -> str:
for character, replacement in CHARACTER_MAP.items(): for character, replacement in CHARACTER_MAP.items():
final = final.replace(character, replacement) final = final.replace(character, replacement)
return f"{final[0:64]}_{random.randint(10_000, 10_000_000_000)}".capitalize() return (
f"{final[0:64]}_{random.randint(-10_000_000_000, 10_000_000_000)}".capitalize()
)
def main() -> int: def main() -> int: