Permalink
Browse files

Remove code duplication.

  • Loading branch information...
1 parent 04c29c6 commit 1e42e3fad17a9772f8e32d1b0929f1b2917f5acb @nrk committed Jul 17, 2010
Showing with 32 additions and 21 deletions.
  1. +2 −6 src/Hpricot.cs
  2. +2 −15 src/HpricotScanner.cs
  3. +28 −0 src/Utilities.cs
View
@@ -30,16 +30,12 @@ public static class Hpricot {
[RubyMethod("buffer_size", RubyMethodAttributes.PublicSingleton)]
public static Int32? GetBufferSize(RubyModule/*!*/ self) {
- Object bufferSize;
- if (self.TryGetClassVariable("@@buffer_size", out bufferSize)) {
- return (int) bufferSize;
- }
- return null;
+ return Utilities.GetBufferSize(self);
}
[RubyMethod("buffer_size=", RubyMethodAttributes.PublicSingleton)]
public static void SetBufferSize(RubyModule/*!*/ self, Int32 bufferSize) {
- self.SetClassVariable("@@buffer_size", bufferSize);
+ Utilities.SetBufferSize(self, bufferSize);
}
}
}
View
@@ -1005,20 +1005,6 @@ public class HpricotScanner {
get { return _procInsParse; }
}
- private static int GetBufferSize(RubyContext context) {
- RubyModule hpricotModule;
- if (!context.TryGetModule(typeof(Hpricot), out hpricotModule)) {
- RubyExceptions.CreateNameError("Cannot find module Hpricot");
- }
-
- Object bufferSize;
- if (hpricotModule.TryGetClassVariable("@@buffer_size", out bufferSize)) {
- return (int)bufferSize;
- }
-
- return DEFAULT_BUFFER_SIZE;
- }
-
private static Object rb_hash_lookup(Hash hash, Object key) {
Object value;
return hash.TryGetValue(key, out value) ? value : null;
@@ -1482,7 +1468,8 @@ public class HpricotScanner {
_state = state;
}
- buffer_size = GetBufferSize(_context);
+ Int32? rubyBufferSize = Utilities.GetBufferSize(_context);
+ buffer_size = rubyBufferSize.HasValue ? rubyBufferSize.Value : DEFAULT_BUFFER_SIZE;
buf = new char[buffer_size];
{
View
@@ -10,5 +10,33 @@ public static class Utilities {
public static MutableString CreateMutableStringFromBuffer(char[] buffer, int raw, int rawlen) {
return MutableString.Create(new String(buffer, raw, rawlen), RubyEncoding.Binary);
}
+
+ public static RubyModule GetHpricotModule(RubyContext context) {
+ RubyModule hpricotModule;
+ if (!context.TryGetModule(typeof(Hpricot), out hpricotModule)) {
+ RubyExceptions.CreateNameError("Cannot find module Hpricot");
+ }
+ return hpricotModule;
+ }
+
+ public static Int32? GetBufferSize(RubyContext context) {
+ return GetBufferSize(GetHpricotModule(context));
+ }
+
+ public static Int32? GetBufferSize(RubyModule hpricotModule) {
+ Object bufferSize;
+ if (hpricotModule.TryGetClassVariable("@@buffer_size", out bufferSize)) {
+ return (int)bufferSize;
+ }
+ return null;
+ }
+
+ public static void SetBufferSize(RubyContext context, Int32 bufferSize) {
+ SetBufferSize(GetHpricotModule(context), bufferSize);
+ }
+
+ public static void SetBufferSize(RubyModule hpricotModule, Int32 bufferSize) {
+ hpricotModule.SetClassVariable("@@buffer_size", bufferSize);
+ }
}
}

0 comments on commit 1e42e3f

Please sign in to comment.