From 79b21c9a897e37226fabf5d9b5064be81efcd19f Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Mon, 14 Jul 2008 12:47:28 +0200
Subject: [PATCH] Stdio: Make getchar() less filling, and inlinable.

---
 lib/modules/Stdio.pmod/module.pmod | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/lib/modules/Stdio.pmod/module.pmod b/lib/modules/Stdio.pmod/module.pmod
index 4fdcb047a5..e52d942e90 100644
--- a/lib/modules/Stdio.pmod/module.pmod
+++ b/lib/modules/Stdio.pmod/module.pmod
@@ -2115,6 +2115,22 @@ class FILE
     bpos=0;
   }
 
+  private protected final int getchar_get_data()
+  {
+    b = "";
+    bpos=0;
+    return low_get_data();
+  }
+
+  private protected final void getchar_updatelinecache()
+  {
+    if(sizeof(cached_lines)>lp+1 && sizeof(cached_lines[lp]))
+      cached_lines = ({cached_lines[lp][1..]}) + cached_lines[lp+1..];
+    else
+      cached_lines = ({});
+    lp=0;
+  }
+
   //! This function returns one character from the input stream.
   //!
   //! @returns
@@ -2123,16 +2139,13 @@ class FILE
   //! @note
   //!   Returns an @expr{int@} and not a @expr{string@} of length 1.
   //!
-  int getchar()
+  inline int getchar()
   {
-    if(sizeof(b) - bpos <= 0 && !get_data())
+    if(sizeof(b) - bpos <= 0 && !getchar_get_data())
       return -1;
 
-    if(sizeof(cached_lines)>lp+1 && sizeof(cached_lines[lp]))
-      cached_lines = ({cached_lines[lp][1..]}) + cached_lines[lp+1..];
-    else
-      cached_lines = ({});
-    lp=0;
+    if(sizeof(cached_lines))
+      getchar_updatelinecache();
 
     return b[bpos++];
   }
-- 
GitLab