Giter VIP home page Giter VIP logo

Comments (8)

iomeone avatar iomeone commented on July 23, 2024

when I switch the prelude to https://github.com/andyarvanitis/purescript-prelude
the example 1 error gone, because eqString using eqStringImpl

auto eqString() -> boxed {
    return Data_Eq::Eq()(Data_Eq::eqStringImpl());
};

but the _indexOf is still not found in ffi, I wonder How to get the indexof right?

from purescript-native.

iomeone avatar iomeone commented on July 23, 2024

https://github.com/LATBauerdick/purescript-native-ffi/blob/be94de6cedbe273d98d7ac3f1addae5158a8e38a/latb/strings.cpp#L101
I found this file implement it !

from purescript-native.

iomeone avatar iomeone commented on July 23, 2024

next unimplement ffi is _unsafeCodePointAt0, I'will work this out tomorrow!

from purescript-native.

andyarvanitis avatar andyarvanitis commented on July 23, 2024

Great, thanks for trying this out. Please see this issue for more information on the prelude refEq issues, and let me know if you need help with the other ffi parts you need to implement for your project.

from purescript-native.

iomeone avatar iomeone commented on July 23, 2024

I write some naive ffi implemetion.
It's very buggy, takes me whole day ....
I need you help! Help me correct the naive code!

FOREIGN_BEGIN( Data_String_Common )

//	split::Pattern->String->Array String

exports["split"] = [](const boxed& p_) -> boxed {
	return [=](const boxed& src_) -> boxed {
		const string& p = unbox<string>(p_);
		const string& s = unbox<string>(src_);

		size_t pos_start = 0, pos_end, delim_len = p.length();
		string token;
		array_t res;
		if (p.length() == 0 || s.length() == 0)
		{
			res.push_back(boxed(s));
			return res;
		}

		if (p.length() > 0 && s.length() > 0)
		{
			while ((pos_end = s.find(p, pos_start)) != string::npos) {
				token = s.substr(pos_start, pos_end - pos_start);
				pos_start = pos_end + delim_len;
				res.push_back(boxed(token));
			}

			res.push_back(boxed(s.substr(pos_start)));
		}
		return res;

	};
};
FOREIGN_END




FOREIGN_BEGIN(Data_String_CodePoints)
//foreign import _unsafeCodePointAt0
//:: (String->CodePoint)
//->String
//->CodePoint

exports["_unsafeCodePointAt0"] = [](const boxed& fallBack) -> boxed {

	return [=](const boxed & str) -> boxed {
		const string& s = unbox<string>(str);
		if (s.length() < 1)
		{
			return fallBack(str);
			//return fallBack(); this code also compile, which one I should use??
		}
		else
		{
			return s[0]; // the code is wrong , bcz chinese character takes 2 or more byte. 
		}
	};

};




exports["_toCodePointArray"] = [](const boxed& fallBack) -> boxed{

	return [=](const boxed & unsafeCodePointAt0) -> boxed {

		return [=](const boxed & str) -> boxed {

			const string& s = unbox<string>(str);
			if (s.length() < 1)
			{
				//printf("fall back\n");
				return fallBack(str);
			}
			else
			{
				array_t result;
			
				for (char c : s) {
					result.push_back(boxed(c));
				}
				return result;
			}
		};
	};

};


// -- Returns the first `n` characters of the string.
// foreign import take :: Int -> String -> String
exports["_take"] = [](const boxed& fallBack) -> boxed {
	return [=](const boxed& n_) -> boxed {
		return [=](const boxed& s_) -> boxed {
			const string& s = unbox<string>(s_);
			auto n = std::max(unbox<int>(n_), 0);
			auto l = s.size();
			return n >= l ? s : (n < 1 ? string("") : s.substr(0, n)); // the code is wrong , bcz chinese character takes 2 or more byte. 
		};
	};

};
FOREIGN_END

from purescript-native.

iomeone avatar iomeone commented on July 23, 2024

charAt ffi:

FOREIGN_BEGIN(Data_String_Unsafe)
//-- | Returns the character at the given index.
//-- |
//-- | **Unsafe:** throws runtime exception if the index is out of bounds.
//foreign import charAt::Int->String->Char


exports["charAt"] = [](const boxed& index_) -> boxed {
	return [=](const boxed& str_) -> boxed {
		const string& str = unbox<string>(str_);
		const int &  index = unbox<int>(index_);

		assert(str.length() >index && index >= 0);
		return str.substr(index, 1);;
	};
};
FOREIGN_END

from purescript-native.

andyarvanitis avatar andyarvanitis commented on July 23, 2024

Ok, I can help with those -- unicode (UTF-8) support has been on my to-do list for a while. I can try to work on it this week (or probably this weekend), and I don't think it should take long, since I already have a general idea of how I will do it.

from purescript-native.

iomeone avatar iomeone commented on July 23, 2024

It'll be fantastic if this project support unicode! looking forward it!

from purescript-native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.